Sunday, November 27, 2022
HomeSoftware DevelopmentWhy cannot a const variable be used to outline an Array's preliminary...

Why cannot a const variable be used to outline an Array’s preliminary measurement in C?


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

What’s an array?

An array is a set of things of the identical information kind saved at contiguous reminiscence areas.

This makes it simpler to calculate the place of every aspect by merely including an offset to a base worth, i.e., the reminiscence location of the primary aspect of the array (usually denoted by the title of the array). The bottom worth is index 0 and the distinction between the 2 indexes is the offset.

Const Variables:

There are a sure algorithm for the declaration and initialization of the fixed variables:

const variable

  • The const variable can’t be left un-initialized on the time of the task.
  • It can’t be assigned worth anyplace in this system.
  • Specific worth wanted to be offered to the fixed variable on the time of declaration of the fixed variable.

Why can’t fixed values be used to outline an array’s preliminary measurement?

In easy phrases, it’s the downside of the C programming language. The sizes of statically-bounded arrays have to be fixed expressions, whereas in C that’s solely one thing like a literal fixed or a sizeof() expression, however not a const-typed variable. The explanations are listed beneath:

  • In C language, a const-qualified variable is just not a relentless expression. A continuing expression is one thing that may be evaluated at compile time – a numeric literal like 10 or 3.14159, a string literal like “Hi there”, a sizeof expression, or some expression made up of the identical like 10 + sizeof( “Hi there”).
  • For array declarations at file scope (exterior the physique of any operate) or as members of struct or union sorts, the array dimension should be a relentless expression.
  • For auto arrays (arrays declared inside the physique of a operate that’s not static), you should use a variable or expression whose worth isn’t identified till runtime, however solely in C99 or later.

Within the following code, const int can’t be used as an array measurement:

C

#embody <stdio.h>

  

const int sz = 0;

typedef struct

{

    char s[sz];

} st;

  

int principal()

{

    st obj;

    strcpy(obj.s, "hey world");

    printf("%s", obj.s);

    return 0;

}

Output:

timeout: didn’t run command ‘./b2204e76-8907-4eb8-a8d6-ce153abe1dc1’: No such file or listing

That is one other deficiency within the C language, which is fastened later in C++. In that language, a const-qualified variable does rely as a relentless expression.

On this case, if we’re utilizing const contained in the principal() operate then const worth can be utilized to initialize array measurement and this system runs with none compile-time or run-time error.

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

int principal()

{

    const int n = 5;

    int arr[n], i;

  

    for (i = 0; i < n; ++i)

        arr[i] = i;

  

    for (i = 0; i < n; i++) {

        cout << arr[i] << " ";

    }

    return 0;

}

Be aware: A continuing variable can be utilized to initialize the array measurement however earlier than declaring an array, the const variable should be assigned some worth. However the information gadgets may be accessible if we’re utilizing const variable. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments