简体   繁体   English

数组中未初始化元素的值是多少?

[英]What will be the value of un-initialized element in the array?

I have the following code 我有以下代码

int t[] = {
        [1] = 45,
        [2] = 33
    };

What will be the value of t[0] in this case? 在这种情况下, t[0]的值是多少? It will contains garbage? 它会包含垃圾吗?

The other value(s) will be initialized; 其他值将被初始化; from C11 standard, §6.7.9 Initialization , ¶19 and ¶21: 从C11标准,§6.7.9 初始化 ,¶19和¶21:

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; 初始化应在初始化器列表顺序中进行,每个初始化器为特定子对象提供,覆盖同一子对象的任何先前列出的初始化器; 151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration. 151)未明确初始化的所有子对象应与具有静态存储持续时间的对象隐式初始化。

151) Any initializer for the subobject which is overridden and so not used to initialize that subobject might not be evaluated at all. 151)子对象的任何初始化程序被覆盖并因此不用于初始化该子对象可能根本不会被评估。


If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration. 如果括号括起的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小的数组的字符串文字中的字符数少于数组中的元素,则聚合的其余部分应为隐式初始化与具有静态存储持续时间的对象相同。

And uninitialized int with static storage duration are implicity initialized to zero; 具有静态存储持续时间的未初始化的int被隐含地初始化为零; same section, ¶10: 同一节,¶10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. 如果未显式初始化具有自动存储持续时间的对象,则其值不确定。 If an object that has static or thread storage duration is not initialized explicitly, then: 如果未显式初始化具有静态或线程存储持续时间的对象,则:

  • if it has pointer type, it is initialized to a null pointer; 如果它有指针类型,则将其初始化为空指针;
  • 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, and any padding is initialized to zero bits; 如果它是一个聚合,则根据这些规则初始化(递归)每个成员,并将任何填充初始化为零比特;
  • if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; 如果它是一个联合,则根据这些规则初始化(递归)第一个命名成员,并将任何填充初始化为零位;

The behavior is the same in C89 and C99. C89和C99中的行为相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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