简体   繁体   中英

Designated initializers and compound literals for struct in C

I have following struct:

typedef struct my_struct {
    int a;
    int b;
    int *c;
} my_struct;

is:

my_struct n = (my_struct) { .b = 3 };

equivalent to:

my_struct n = (my_struct) { .a = 0, .b = 3, .c = NULL };

What about:

my_struct n = (my_struct) { .b = 3, 0 };

They shall be initialized as if they were static, we can find this in the draft C99 standard section 6.7.8 Initialization paragraph 19 says ( emphasis mine ):

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.

If the following initializer is not a designator then it will pick up with the next field after that designator, which is covered in paragraph 17 :

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)

This applies recursively to subaggregates as per paragraph 20 :

If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions

The rules for initializing objects of static duration are found in section 6.7.8 paragraph 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; [...]

Is my_struct n = (my_struct) { .b = 3 }; equivalent to my_struct n = (my_struct) { .a = 0, .b = 3, .c = NULL }; ?

Yes. A compound literal may fail to provide full initialization, in which case any unimitialized members initialize to zero ( NULL is case of pointer member) by default .

What about my_struct n = (my_struct) { .b = 3, 0 }; ?

Member b and c will be initialized to 3 and 0 respectively while a will be initialized to 0 by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM