[英]Initializing pointer arrays to NULL within a struct
Given the struct declaration (where data
is an array of pointers to another struct): 给定struct声明(其中
data
是指向另一个struct的指针数组):
typedef struct {
B* data[10];
} A;
It seems that pointer arrays within structs are not automatically initialized to NULL: 似乎结构中的指针数组不会自动初始化为NULL:
struct A myA;
myA.data[0] == NULL (false!)
Is there a way to enforce that the array is initialized to NULLs without doing it explicitly? 有没有一种方法可以强制将数组初始化为NULL而不显式地将其初始化?
An object of any type can be initialized to zero: 任何类型的对象都可以初始化为零:
struct A myA = { 0 };
This initializes the first scalar member or element (defined recursively for nested arrays, structures, and unions) to 0
converted to the appropriate type, which is a null pointer value if it's a pointer. 这会将第一个标量成员或元素(为嵌套数组,结构和联合递归定义)初始化为
0
转换为适当的类型,如果它是指针,则为空指针值。 All remaining members or elements are also initialized to zero. 所有剩余的成员或元素也都初始化为零。
Some compilers may warn you about the missing initializers; 一些编译器可能会警告您有关缺少的初始化程序的信息; since
= { 0 }
is, or should be a common idiom for "initialize everything to zero", you can ignore the warning. 由于
= { 0 }
是或应该是“将所有内容初始化为零”的常用用法,因此可以忽略该警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.