C99 Structure Designated Initialisers and other value
Asked Answered
A

3

14

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;
};

So following is valid :

struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'};

Also it is said that uninitialised members will be set to 0. So

struct myStruct m = {.f = 10.11, .c = 'a'};

here i will be set to 0

But, for the following :

struct myStruct m = {.f = 10.11, .c = 'a', 6}; 

i is still initialized to 0. What is the reason if we do such compound initialization.

Ancipital answered 19/8, 2014 at 15:2 Comment(0)
S
14

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.

Someone answered 19/8, 2014 at 15:5 Comment(0)
I
6

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.

Infectious answered 19/8, 2014 at 15:5 Comment(3)
so even if I had initialized f with 10.11 now it would be set to 6, right ?Ancipital
@Sagrian; Yes. The older value will be overwritten.Infectious
gcc warns about this with -Wextra or -Woverride-init.Tallyman
Q
0

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.

Quinlan answered 19/8, 2014 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.