[英]Warning when trying to initialize a 2D struct array with two initializer lists
I am trying to initialize a 2D array of user defined type to zero by using following line, 我试图通过使用以下行将用户定义类型的2D数组初始化为零,
qmf_t X_hybrid_left[32][32] = {{0}};
Where qmf_t
is a user defined type. 其中
qmf_t
是用户定义的类型。 Here I get the compiler warning, 在这里我得到编译器警告,
warning: missing braces around initializer [-Wmissing-braces]"
警告:初始化程序周围缺少大括号[-Wmissing-braces]“
But if I use, qmf_t X_hybrid_left[32][32] = {{{0}}};
但如果我使用,
qmf_t X_hybrid_left[32][32] = {{{0}}};
, ie 3 braces each side, warning disappears. ,即每侧3个括号,警告消失。
Is it correct to use three braces on each side? 每侧使用三个支架是否正确? What does it mean?
这是什么意思?
qmf_t X_hybrid_left[32][32] = { /* Row initializers next */
{ /* Col initializers next */
{ /* Struct initializers next */
0
}
}
};
qmf_t a = {0};
qmf_t b[5] = {{0}};
qmf_t c[10][5] = {{{0}}};
From C11 specs, 6.7.9 Initialization grammar 从C11规范, 6.7.9初始化语法
initializer:
初始化:
assignment-expression赋值表达式
{ initializer-list }{initializer-list}
{ initializer-list , }{initializer-list,}
Although in your particular case (zeroing all objects of 2 arrays), qmf_t X_hybrid_left[32][32] = {0};
虽然在您的特定情况下(将2个数组的所有对象归零),但
qmf_t X_hybrid_left[32][32] = {0};
will work same as qmf_t X_hybrid_left[32][32] = {{{0}}};
将与
qmf_t X_hybrid_left[32][32] = {{{0}}};
相同qmf_t X_hybrid_left[32][32] = {{{0}}};
but compiler may warn you. 但编译器可能会警告你。
But if you want any non-zero initialization, you need to use multiple braces. 但是如果你想要任何非零初始化,你需要使用多个大括号。
From the same section: 从同一部分:
[16] Otherwise, the initializer for an object that has aggregate or union type shall be a brace enclosed list of initializers for the elements or named members.
[16]否则,具有聚合或联合类型的对象的初始值设定项应为元素或命名成员的大括号括起来的初始值设定项列表 。
[20] If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions.
[20]如果聚合或联合包含聚合或联合的元素或成员,则这些规则将递归地应用于子聚合或包含的联合。 If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the contained union.
如果子集合或包含的并集的初始值设定项以左括号开头,则由该括号括起的初始值设定项及其匹配的右括号初始化子集合或所包含的并集的元素或成员。 Otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union;
否则,仅从列表中获取足够的初始值设定项以考虑子集合的元素或成员或所包含的并集的第一个成员; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.
任何剩余的初始值设定项都用于初始化当前子聚合或包含的union所属的聚合的下一个元素或成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.