When we use the new struct initialization format, do unlisted members have an unspecified value?
c
Solution
They are initialized to zero.
From the C99 standard, §6.7.8, item 19:
The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding anypreviously listed initializer for the same subobject; all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
The rules for initialization of `static` objects are specified earlier in item 10:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
Problem
So I have a large struct that I need to prepare: ``` struct Config { int a; int b; int c; struct { int x; int y; } d[40]; }; ``` I'd like to fill this as such: ``` Config config = { .a = 3; .b = 4; .d[0] = {10, 12}; .d[1] = {14, 16}; }; ``` After this, does the value of `config.c` and `config.d[2]` have an unspecified value? or is it zero? Or, am I required to do: ``` Config config; memset(&config, 0, sizeof(config)); config.a = 3; config.b = 4; config.d[0].x = 10; config.d[0].y = 12; ... ```