0

I want to understand the difference between initializing a 1D array with a variable for it's size Vs. using just a literal for the size.

In a course I'm following, the instructor spoke about how if we declare an array to have 5 elements but only initialize it with 3 elements, the compiler will initialize the other two elements with 0. So, I tried the following - in the first case, I declare a size variable.

int size = 5;
int scores[size] = {4, 6, 2};
for (int i = 0; i < size; ++i)
{
    printf("%d ", scores[i]);
}
printf("\n\n");

However, running this yields the following error -

variable-sized object may not be initialized

I checked this post but I don't believe I understand why the code works when I just use the constant literal (5) like so -

int data[5] = {4, 6, 2};
for (int i = 0; i < 5; ++i)
{
    printf("%d\t", data[i]);
}
printf("\n\n");

The above code outputs the three elements and two zeros just as the instructor mentioned.

My question is what is the difference in the above two code snippets.

nobis
  • 1
  • 2
  • I included that link in the question. I don't believe it explains why the second code works. – nobis Aug 27 '23 at 19:57
  • 1
    The first is a variable-length array, which can't be initialized at compile time, and is not default-initialized at runtime. – stark Aug 27 '23 at 20:01
  • So, why isn't the second one a variable-length array, as this too has just three initial elements? – nobis Aug 27 '23 at 20:09
  • Because 5 is not a variable. – stark Aug 27 '23 at 20:09
  • `5` is a compile time constant integer literal, `size` is a runtime non constant integer lvalue. When you compile your code, the compiler knows the size in the second snippet and can ensure that you will be able to initialize it properly, however in the first example it does not know the size (you could change it to idk 1 before creating the array) and initialization might fail. – Joel Aug 27 '23 at 20:17
  • 1
    *this too has just three initial elements* `int data[5]` has five elements, always and forever. When you initialised the first three elements, the other two elements are implicitly initialised to `0`. – Weather Vane Aug 27 '23 at 20:35
  • @Joel Thanks for your comment. So, I changed the `size` variable to have the `const` modifier, but I still get the same error. Shouldn't declaring `size` as `const` be enough for the compiler to know that the size won't change further in the code? – nobis Aug 27 '23 at 20:59
  • This question has been marked as duplicate but the original one doesn't exactly answer my question completely. I finally figured out that this is a quirk in the behavior of `gcc` in Linux. Even if I initialize the array `int scores[size] = {1, 2, 3, 4, 5}` with all five elements, (I declared `size` to be a `const int`) it produces the error - `variable-sized object may not be initialized`. However, compiling the same code with `clang` in Linux works perfectly fine. – nobis Aug 27 '23 at 22:07
  • 2
    GCC is following standard C more closely than Clang, then. – Jonathan Leffler Aug 27 '23 at 23:45
  • @nobis What was left unanswered by the duplicate? I added an answer including the changed rules for initialization of VLAs in the (upcoming) C23 standard to [the duplicate](https://stackoverflow.com/a/76988959/7582247). – Ted Lyngmo Aug 28 '23 at 14:54
  • @WeatherVane [One! Two! Five!](https://www.youtube.com/watch?v=xOrgLj9lOwk) :-D – Andrew Henle Aug 28 '23 at 15:09
  • @AndrewHenle or the number of the Inquisition's weapons? – Weather Vane Aug 28 '23 at 15:37
  • @WeatherVane That was unexpected. – Andrew Henle Aug 28 '23 at 19:43

0 Answers0