c - 初始化 const 结构时重复字段

标签 c

抱歉可能有重复。我不知道如何用谷歌搜索它。

const 结构体的初始化列表中是否允许重复字段?

示例:

const struct first _first = {
    .value = 1,
    .value = 2, /* same field again */
};

const struct second _second = {
    .first = _first,
    .first.value = 3, /* and again */
};

第二种情况当然更重要。

最佳答案

这是允许的,后面的初始化程序会覆盖之前的任何初始化程序。

这在 C 标准第 6.7.9p19 节中指定:

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; 151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

151) Any initializer for the subobject which is overridden and so not used to initialize that subobject might not be evaluated at all.

关于c - 初始化 const 结构时重复字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52544818/

相关文章:

链接器可以内联函数吗?

c - 如何通过检查分隔符在字符串中添加字符?

c - 在 C 中编程以读取参数

c - 为简单设备驱动程序编译内核模块时出错

c - 删除 C 中重复的整数对的有效方法

c - 递归函数添加数字序列的总和及其总和

c - setuid() 失败 - 不允许操作

c - C中的L值与R值

c - 在 c 中使用箭头运算符运行 chdir 然后打印的段错误

c - Valgrind:是否有内存泄漏?