The C standard allows an array variable to be declared both with a dimension index and with an initialization literal. The initialization literal also implies an array sizedimension, in the number of elements specified.
...
Code Block |
---|
int array[] = {1, 2, 3}; /* 3-element array */ |
Wiki Markup |
---|
but it is possible to use designators to initialize array elements in a non-contiguous fashion. C99 Section 6.7.8, " |
Initialization" states \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\]: |
Space can be "allocated" from both ends of an array by using a single designator:
Code Block int a[MAX] = { 1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0 };In the above, if
MAX
is greater than ten, there will be some zero-valued elements in the middle; if it is less than ten, some of the values provided by the first five initializers will be overridden by the second five.
...
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.8, "Initialization" |
...
ARR01-A. Do not apply the sizeof operator to a pointer when taking the size of an array 06. Arrays (ARR) ARR30-C. Guarantee that array indices are within the valid range