Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 };
like image 378
zodiac Avatar asked Jul 24 '14 14:07

zodiac


People also ask

What are compound literals in C?

In C, a compound literal designates an unnamed object with static or automatic storage duration. In C++, a compound literal designates a temporary object that only lives until the end of its full-expression.

What is designated initialization in C?

Designated initializers, a C99 feature, are supported for aggregate types, including arrays, structures, and unions. A designated initializer, or designator, points out a particular element to be initialized. A designator list is a comma-separated list of one or more designators.

Does C++ have compound literals?

In C++, a compound literal designates a temporary object, which only lives until the end of its full-expression. As a result, well-defined C code that takes the address of a subobject of a compound literal can be undefined in C++, so the C++ compiler rejects the conversion of a temporary array to a pointer.

What is struct initialization?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).


1 Answers

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; [...]

like image 87
Shafik Yaghmour Avatar answered Sep 20 '22 13:09

Shafik Yaghmour