...
Code Block |
---|
int array[] = {1, 2, 3}; /* 3-element array */ |
Wiki Markup |
---|
but it is also possible to use designators to initialize array elements in a non-contiguousnoncontiguous fashion. C99 Section 6.7.8, "Initialization" states \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] states: |
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.
...
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
and And paragraph 22 states:
If an array of unknown size is initialized, its size is determined by the largest indexed element with an explicit initializer. At the end of its initializer list, the array no longer has incomplete type.
...
Note that this recommendation does not apply (in all cases) to character arrays initialized with string literals, see STR36-C. Do not specify the bound of a character array initialized with a string literal for more information.
...
Code Block | ||
---|---|---|
| ||
int a[3] = {1, 2, 3, 4}; |
The size of the array a
is three3, although the size of the initialization is four4. The last element of the initialization (4
) is ignored. Most compilers will diagnose this error.
...