C Reference ยท Types and Literals
Designated initializers in C
Reference for C99 designated initializers: set struct members by name, array elements by index, nested designators, mixing with positional values and zero fill.
A designated initializer names the member or array element it initializes. Instead of relying on the order of fields, you write .name = value for a struct or union member and [index] = value for an array element. Everything you do not mention is set to zero.
Designators make initializers self-documenting and keep them correct when a struct gains new fields. They are one of the features that make C99 and later feel noticeably nicer than K&R C.
struct user { const char *name; int age; bool admin; }; struct user u = { .name = "Ada", .admin = true }; /* age is 0 */ int hits[10] = { [3] = 7, [9] = 1 };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
struct S s = { .member = expr, .other.nested = expr }; union U u = { .member = expr }; type a[N] = { [index] = expr, [lo ... hi] = expr }; /* range: GCC extension */
| Part | Description |
|---|---|
.member |
Selects a named struct or union member. Chain for nested: .outer.inner. |
[index] |
Selects an array element; the index must be a constant expression. |
| Following values | Positional values after a designator continue from the next member or element. |
Errors / undefined behavior: an unknown member name or an index out of range is a constraint violation (compile error). Initializing the same member twice is allowed, but the later one wins, and any side effects in the earlier expression may or may not happen.
Usage notes
- Unmentioned members are zero (or null pointers), even padding in most compilers; this makes
struct S s = { .a = 1 };a handy way to zero the rest. - Order is free in C, unlike C++20 where designators must follow declaration order. Keep declaration order anyway if the code may be compiled as C++.
- Array length with designators equals the highest index plus one, if you omit the size.
int a[] = { [9] = 1 };has 10 elements. - Index with enums to build lookup tables that survive reordering of the enum.
- Unions: a designator picks which member is active; without one, the first member is initialized.
- Ranges (
[2 ... 5] = 0) are a GCC and Clang extension, not standard C, and need spaces around.... - Compound literals accept the same initializers:
(struct user){ .name = "Bob" }. - Do not mix up the syntax:
.name = xuses a dot and an equals sign, while GCC's oldname: xform is obsolete.
Related
- Compound literals, Arrays,
struct,union,enum - Lessons: Structures, Unions, Enums