在 C99 中(而不是在 C++ 中),可以使用以下语法初始化结构:
struct info
{
char name[8+1];
int sz;
int typ;
};
struct info arr[] =
{
[0] = { .sz = 20, .name = "abc" },
[9] = { .sz = -1, .name = "" }
};
未指定的字段会怎样?
最佳答案
它们归零了。来自 C99 标准 §6.7.8(初始化)/21,
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.
关于c - 未由指定初始值设定项命名的字段会发生什么情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3374446/