Designated initializers, a C99 feature, are supported for aggregate types, including arrays, structures, and unions. A designated initializer, or designator, points out a particular element to be initialized. A designator list is a comma-separated list of one or more designators. A designator list followed by an equal sign constitutes a designation.
Designator list syntax for structures and unions .-,------------------------. V | >>-{----.--member -=--expression-+--}--------------------------><
Designator list syntax for arrays .-,----------------------------------------. | .-----------------------. | V V | | >>-{------[--array subscript--]-+--=--expression-+--}----------><
union { /* … */ } caw = { .any_member = 13 };
struct xyz {
int a;
int b;
int c;
} klm = { .a = 99, .c = 100 };
int aa[4] = { [2] = 3, [1] = 6 };
static short grid[3] [4] = { [0][0]=8, [0][1]=6,
[0][2]=4, [0][3]=1,
[2][0]=9, [2][1]=3,
[2][2]=1, [2][3]=1 };
The
omitted four elements of grid are initialized to
zero: Element | Value | Element | Value |
---|---|---|---|
grid[0] [0] | 8 | grid[1] [2] | 0 |
grid[0] [1] | 6 | grid[1] [3] | 0 |
grid[0] [2] | 4 | grid[2] [0] | 9 |
grid[0] [3] | 1 | grid[2] [1] | 3 |
grid[1] [0] | 0 | grid[2] [2] | 1 |
grid[1] [1] | 0 | grid[2] [3] | 1 |
int a[10] = {2, 4, [8]=9, 10}
In
this example, a[0] is initialized to 2, a[1] is
initialized to 4, a[2] to a[7] are
initialized to 0, and a[9] is initialized to 10.int a[MAX] = {
1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0
};
The designated initializer, [MAX-5] = 8,
means that the array element at subscript MAX-5 should
be initialized to the value 8. If MAX is 15, a[5] through a[9] will
be initialized to zero. If MAX is 7, a[2] through a[4] will
first have the values 5, 7, and 9,
respectively, which are overridden by the values 8, 6,
and 4. In other words, if MAX is
7, the initialization would be the same as if the declaration had
been written: int a[MAX] = {
1, 3, 8, 6, 4, 2, 0
};
struct a {
struct b {
int c;
int d;
} e;
float f;
} g = {.e.c = 3 };
initializes member c of structure variable e,
which is a member of structure variable g, to the
value of 3.