c - 未由指定初始值设定项命名的字段会发生什么情况?

标签 c initialization designated-initializer

在 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/

相关文章:

c++ - 如何检测全局与链路本地 IPv6 地址

c++ - ArrayList 的大小未知帮助

c++ - 这种类型的初始化叫什么?

java - 如何通过NAT通过公网IP发送UDP数据包?

c - C中如何使用全局变量在不同的函数中使用同一个变量

c - 错误 C4703 和错误 4716

C++ vector 初始化?

Javascript初始化和错误处理

c - 使用指定的初始值设定项来初始化具有 `char []` 成员的嵌套结构

c - 具有指定初始化程序的 MSVC12 (VS2013) 中可能存在编译器错误