C99 Structure Designated Initialisers and other va

2019-02-21 08:53发布

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.

3条回答
可以哭但决不认输i
2楼-- · 2019-02-21 09:02

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.

查看更多
【Aperson】
3楼-- · 2019-02-21 09:07

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.

查看更多
Explosion°爆炸
4楼-- · 2019-02-21 09:07

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.

查看更多
登录 后发表回答