Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization of C array at time other than declaration?

I know in C that I can do the following.

int test[5] = {1, 2, 3, 4, 5};

Now this is only legal when declaring the array. However I was wondering why this is not legal to do later? But then later in the program it is not legal to do the following.

test[5] = {10, 20, 30, 40, 50}; 

Or something similar. Why is this? I know it's not legal, and I'm not complaining, but could someone give me a more technical explanation of why I can't do this? (i.e. don't just say the C spec does not allow it or something like that)

I'm assuming it has to do something with the time when memory gets allocated on the stack for the array, so at that point C can auto fill in my values, but then why can't it do it later?

Thanks guys

like image 589
John Avatar asked Mar 05 '12 15:03

John


People also ask

What are the different ways of initializing array in C?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.

Is it necessary to initialize the array at the time of declaration in C?

It is necessary to initialize the array at the time of declaration. This statement is false.

Are Arrays in C automatically initialized?

Please note that the global arrays will be initialized with their default values when no initializer is specified. For instance, the integer arrays are initialized by 0 . Double and float values will be initialized with 0.0 .


1 Answers

It's not just arrays, you cannot provide an initializer for anything at any point other than in a definition. People sometimes refer to the second statement of something like int i; i = 0; as "initializing i". In fact it's assigning to i, which previously holds an indeterminate value because it wasn't initialized. It's very rarely confusing to call this "initializing", but as far as the language is concerned there's no initializer there.

Assignment and initialization are separate things to the language, even though they both use the = character. Arrays are not assignable.

The reason arrays are not assignable is covered elsewhere, for example Why does C++ support memberwise assignment of arrays within structs, but not generally?. The short answer is, "historical reasons". I don't think there's any killer technical reason why the language could not be changed to allow array assignment.

There's a secondary issue, that grammatically {1, 2, 3, 4, 5} is an initializer, not an array literal, and hence could not be used in assignment even if arrays were assignable. I'm not sure exactly why C89 doesn't have array literals, probably just nobody got around to requiring them. C99 introduces a syntax for "compound literals" in general and array literals in particular: (int[]) {1, 2, 3, 4, 5}. You still can't assign to an array from it.

like image 186
Steve Jessop Avatar answered Oct 24 '22 07:10

Steve Jessop