C99标准支持结构体按成员名称进行指定初始化。

来源:互联网 发布:淘宝网佛经早晚课读本 编辑:程序博客网 时间:2024/06/05 07:15

今天和同事讨论发现,C99支持对结构体进行指定成员的初始化方式了,前阵子在python上见过,感觉很方便,现在C语言也支持了,需要注意的是,要求编译器支持C99的特性。


下面是C99的原文描述,在6.7.8 Initialization 章节有如下描述:
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.127)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.128)

下面是C99给出的举例:
34 EXAMPLE 10 Structure members can be initialized to nonzero values without depending on their order:
div_t answer = { .quot = 2, .rem = -1 };

0 0
原创粉丝点击