36 c static struct initialization
我有一个关于C中静态变量初始化的问题.我知道如果我们声明一个全局静态变量,默认情况下该值是0.例如:
static int a; //although we do not initialize it, the value of a is 0
Run Code Online (Sandbox Code Playgroud)
但是以下数据结构如何:
typedef struct
{
int a;
int b;
int c;
} Hello;
static Hello hello[3];
Run Code Online (Sandbox Code Playgroud)
是所有成员中的每一个结构hello[0],hello[1],hello[2]初始化为0?
pmg*_*pmg 51
是的,所有成员都被初始化为具有静态存储的对象.见C99标准中的6.7.8/10 (PDF文件)
如果未显式初始化具有自动存储持续时间的对象,则其值不确定.如果没有显式初始化具有静态存储持续时间的对象,则:
- 如果它具有指针类型,则将其初始化为空指针;
- 如果它有算术类型,则初始化为(正或无符号)零;
- 如果是聚合,则根据这些规则初始化(递归)每个成员;
- 如果它是一个联合,则根据这些规则初始化(递归)第一个命名成员.
要将对象中的所有内容(无论是否)初始化static为0,我都希望使用通用零初始值设定项
sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45752 次 |
| 最近记录: |