liveBook · Manning

Aggregate data type

As explained in Modern C

An aggregate data type is a C type that groups multiple component objects, such as an array, structure, or union.

Consider three arrays initialized with different forms: `double A[] = { 7.8, };`, `double B[3] = { 2 * A[0], 7, 33, };`, and `double C[] = { [0] = 6, [3] = 1, };`. Each object groups several component objects, while its initializer assigns values to those components. Such grouped C types are aggregate data types. section 5.5

Arrays combine items with one base type; structures combine items that may have different base types. Unions group differently typed members over the same memory location, but their treatment requires a deeper memory-model discussion. Aggregate initialization follows declaration order when designators are omitted, and omitted members receive default initialization. section 6.0 section 5.5

Key rules make aggregate initialization predictable:

  • Prefer designated initializers such as `[3] = 1` or `.member = value`; they remain more robust when declarations change. section 5.5
  • Use `{0}` as a catch-all initializer for every non-variable-length object type; the first component receives zero and the remaining components default to zero. section 5.5
double A[] = { 7.8, };
double B[3] = { 2 * A[0], 7, 33, };
double C[] = { [0] = 6, [3] = 1, };


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":6}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":6}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":6}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":6}]]"}
!@%STYLE%@!

Compare inferred array length, positional initialization, and designated array elements in a compact code listing. section 5.5

How Modern C covers it

How deep it goes
Defines arrays and structures, explains initializer behavior, and defers unions and pointer internals to later treatment.
Examples
Uses array declarations with inferred lengths, positional values, expressions, trailing commas, and designated element indexes.
What it assumes
Assumes familiarity with basic C types and variables while introducing arrays and structures as derived types.
What you can do after
Apply C initializer rules to construct and safely initialize arrays and other aggregate objects.
How it connects
Connects arrays, structures, unions, pointers, typedef, and designated initializers within C’s derived-type system.

Related concepts

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