Array initialization
When initializing an object of array type, the initializer must be either a string literal (optionally enclosed in braces) or be a brace-enclosed list of initialized for array members:
# Notes
The order of evaluation of subexpressions in an array initializer is indeterminately sequenced in C (but not in C++ since C++11):
In C, the braced list of an initializer cannot be empty. C++ allows empty list:
An empty initializer can be used to initialize an array:
As with all other initialization, every expression in the initializer list must be a constant expression when initializing arrays of static or thread-local storage duration:
# Example
int main(void)
{
// The following four array declarations are the same
short q1[4][3][2] = {
{ 1 },
{ 2, 3 },
{ 4, 5, 6 }
};
short q2[4][3][2] = {1, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 4, 5, 6};
short q3[4][3][2] = {
{
{ 1 },
},
{
{ 2, 3 },
},
{
{ 4, 5 },
{ 6 },
}
};
short q4[4][3][2] = {1, [1]=2, 3, [2]=4, 5, 6};
// Character names can be associated with enumeration constants
// using arrays with designators:
enum { RED, GREEN, BLUE };
const char *nm[] = {
[RED] = "red",
[GREEN] = "green",
[BLUE] = "blue",
};
}