I have a variable of type int array[10]
. Is it possible to initialize only the last item of the array?
An array can also be initialized after declaration. Note: When assigning an array to a declared variable, the new keyword must be used.
It is necessary to initialize the array at the time of declaration. This statement is false.
Yes, using designated initializers (introduced in C99), you can write code like this:
int array[10] = {[9] = 42};
which is equivalent to:
int array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 42};
This feature is also available in some compiler as an extension, for instance, GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With