Wednesday, November 30, 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 knowledge sort saved at contiguous reminiscence areas.

This makes it simpler to calculate the place of every component by merely including an offset to a base worth, i.e., the reminiscence location of the primary component 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 supplied 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 disadvantage 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 under:

  • In C language, a const-qualified variable will not be a relentless expression. A relentless expression is one thing that may be evaluated at compile time – a numeric literal like 10 or 3.14159, a string literal like “Hiya”, a sizeof expression, or some expression made up of the identical like 10 + sizeof( “Hiya”).
  • For array declarations at file scope (exterior the physique of any perform) 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 perform that isn’t static), you should utilize a variable or expression whose worth isn’t recognized till runtime, however solely in C99 or later.

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

C

#embrace <stdio.h>

  

const int sz = 0;

typedef struct

{

    char s[sz];

} st;

  

int important()

{

    st obj;

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

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

    return 0;

}

Output:

timeout: did not 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 important() perform then const worth can be utilized to initialize array measurement and this system runs with none compile-time or run-time error.

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int important()

{

    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;

}

Word: A relentless variable can be utilized to initialize the array measurement however earlier than declaring an array, the const variable have to be assigned some worth. However the knowledge objects 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