A designated initializer assigns a value to a specific element of an aggregate by naming its position before the value. For arrays, the designation is an index in brackets, such as [4] = 3.E+25. section 2.3
Selecting array elements explicitly
double A[5] = { [0] = 9.0, [1] = 2.9, [4] = 3.E+25, [3] = .00007, }; The omitted element A[2] becomes 0.0. Array indices begin at 0, so [4] selects the fifth and final element. section 2.3
Designated initializers are especially useful for aggregate data types because each value remains attached to its intended element when declarations change. An unsized array can also derive its length from the highest designated position. section 5.5
Key rules:
- Use [index] = value to select an array element explicitly. section 2.3
- Leave an element undesignated and its initialized value defaults to zero. section 2.3
- Prefer designated initializers for aggregate data types when declaration changes could otherwise shift positional values. section 5.5
