抱歉可能有重复。我不知道如何用谷歌搜索它。
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/