c / intermediate
Snippet
Designated Initializers for Structs
Designated initializers allow you to initialize specific members of a struct by name, regardless of their order in the definition. Any members not explicitly initialized are set to zero.
snippet.c
1
2
3
4
5
6
7
8
9
10
struct Config {int id;char mode;float ratio;};struct Config c = {.id = 42,.ratio = 0.75f};
Breakdown
1
.id = 42,
Explicitly sets the 'id' field to 42.
2
.ratio = 0.75f
Sets the 'ratio' field, skipping 'mode' which defaults to 0.