liveBook · Manning

Designated initializer

As explained in Modern C, Third Edition

A designated initializer is an initializer that explicitly selects an array element or structure member by designation before assigning its value.

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

How Modern C, Third Edition covers it

How deep it goes
Explains bracket syntax, index numbering, omitted-element defaults, and inferred array length without descending into compiler implementation.
Examples
Demonstrates a five-element double array with designated indices, omitted elements, and an inferred-length array.
What it assumes
Assumes basic C declarations and arrays while establishing zero-based indexing and aggregate-initializer behavior.
What you can do after
Build it from scratch by writing explicit, maintainable initializers for arrays and other aggregate data types.
How it connects
Connects array initialization with aggregate data types, array indices, incomplete array types, and default initialization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.