指定的初始值以及在C结构复合文字 [英] Designated initializers and compound literals for struct in C

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

问题描述

我有以下结构:

  typedef结构my_struct {
    int类型的;
    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 的说(的重点煤矿的):


  

初​​始化应发生在初始化列表中的顺序,每个
  初始化提供了一个特定的子对象覆盖所有
  previously上市初始化为相同的子对象; 132)所有
  未明确初始化子对象应被初始化
  隐含一样具有静态存储持续时间的对象。


如果下面的初始化不是一个标志,然后它将搭载与标志之后的下一个领域,这是覆盖在段落的 17 的:


  

每个大括号内的初始化列表有一个相关的当前对象。
  如果没有指定值为present,当前对象的子对象
  根据当前对象的类型初始化顺序:
  增加下标顺序数组元素,结构成员
  声明顺序和union.129的第一个命名成员)在
  相反,指定导致以下初始化开始
  由标志所描述的子对象的初始化。
  初始化然后继续前进,才能,较年初
  之后由designator.130描述下一子对象)


这递归适用于subaggregates为每个段落的 20 的:


  

如果该聚集或联合包含的元件或部件
  聚合或工会,这些规则递归地应用到
  subaggregates或含有工会


初​​始化静态持续时间的对象的规则在部分找到 6.7.8 段的 10 的:


  

如果具有自动存储生存期的对象未初始化
  明确地说,它的价值是不确定的。如果具有静态对象
  存储持续时间不明确初始化,然后:


  
  

- 如果它有指针的类型,它被初始化为空指针;


  
  

- 如果它具有算术类型,它被初始化为(正或无符号的)零;


  
  

- 如果它是集料,每件被初始化(递归地)根据这些规则;
  [...]


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

这篇关于指定的初始值以及在C结构复合文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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