C 中 struct 的指定初始化器和复合字面量 [英] Designated initializers and compound literals for struct in C

查看:40
本文介绍了C 中 struct 的指定初始化器和复合字面量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

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

是:

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

相当于:

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

怎么样:

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

推荐答案

它们应该像静态一样被初始化,我们可以在草案 C99 标准部分 6.7.8 初始化19说(强调我的):

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):

初始化应按初始化列表顺序进行,每个为覆盖任何特定子对象提供的初始化程序先前列出的同一子对象的初始化程序;132) 全部应初始化未显式初始化的子对象隐含地与具有静态存储持续时间的对象相同.

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.

如果下面的初始化器不是一个指示符,那么它将使用该指示符之后的下一个字段,这在第 17 段中有所介绍:

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:

每个用大括号括起来的初始化列表都有一个关联的当前对象.当没有指定时,当前对象的子对象是根据当前对象的类型依次初始化:数组元素按递增下标顺序,结构成员在声明顺序,以及联盟的第一个命名成员.129) 在相反,指定会导致以下初始化程序开始由指示符描述的子对象的初始化.然后初始化按顺序继续向前,从指示符所描述的子对象之后的下一个子对象.130)

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)

这递归地适用于根据段落 20 的子聚合:

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

静态持续时间对象的初始化规则见6.7.810段:

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

这篇关于C 中 struct 的指定初始化器和复合字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆