I am aware that in C99 you can initialize members of the structure using member name as follows :
struct myStruct
{
int i;
char c;
float f;
};
This is covered in the draft C99 standard section 6.7.8
Initialization, basically if the following initializer is not a designator then it will pick up with the next field after that designator, which for your examples would be f
. We can look at paragraph 17 which says (emphasis mine):
Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.129) In contrast, a designation causes the following initializer to begin initialization of the subobject described by the designator. Initialization then continues forward in order, beginning with the next subobject after that described by the designator.130)
Why i
is initialized to 0
is covered in paragrah 19 which says:
The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;132) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
Note that as Keith points out gcc
provides a warning for this using -Wextra
:
warning: initialized field overwritten [-Woverride-init]
struct myStruct m = {.f = 10.11, .c = 'a', 6};
^
and clang
seems to warn about this by default.
Here, 6 is non-designated initializer. So, this value is initialized to the member just after the previous designated initializer, that is the float just after char.
In case you had two or more non-designated initializers in series, then the non-designated initializers would be initialized to the members in series from last designated initializer.
In case of
struct myStruct = {.f = 10.11, .c = 'a', 6};
the value 6
which is non-designated initializer will assign to the member just after the member initialized with designated initializer. So, in this case, member f
is just after c
and hence it will be initialized to 6
. i
still will be initialized to 0
by default.