且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C struct自动初始化值,数组初始化

更新时间:2021-08-27 21:05:24

这要视情况而定.除非明确初始化,否则

It depends. Unless initialized explicitly,

  • 如果该变量具有静态(或线程)存储持续时间,则成员将被初始化为0或等效值.

在具有自动存储期限的情况下,内容将不确定(是的,不确定" 垃圾" 更合适)./p>

In case it has automatic storage duration, the contents will be left indeterminate (yes, "indeterminate" is more appropriate than "garbage").

引用C11,第§6.7.9/p10

Quoting C11, chapter §6.7.9/p10

如果未自动初始化具有自动存储期限的对象,则其值为 不定.如果具有静态或线程存储持续时间的对象未初始化 明确地,然后:

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 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;