尝试使用两个初始化列表初始化二维结构数组时发出警告
Warning when trying to initialize a 2D struct array with two initializer lists
我正在尝试使用以下行将用户定义类型的二维数组初始化为零,
qmf_t X_hybrid_left[32][32] = {{0}};
其中 qmf_t
是用户定义的类型。这里我得到了编译器警告,
warning: missing braces around initializer [-Wmissing-braces]"
但是如果我使用 qmf_t X_hybrid_left[32][32] = {{{0}}};
,即每边 3 个大括号,警告就会消失。
每侧使用三个支架是否正确?这是什么意思?
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}}};
来自 C11 规范,6.7.9 初始化 语法
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
尽管在您的特定情况下(将 2 个数组的所有对象归零),qmf_t X_hybrid_left[32][32] = {0};
的工作方式与 qmf_t X_hybrid_left[32][32] = {{{0}}};
相同,但编译器可能会警告您。
但是如果你想要任何非零初始化,你需要使用多个大括号。
来自同一部分:
[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.
[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. 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.
我正在尝试使用以下行将用户定义类型的二维数组初始化为零,
qmf_t X_hybrid_left[32][32] = {{0}};
其中 qmf_t
是用户定义的类型。这里我得到了编译器警告,
warning: missing braces around initializer [-Wmissing-braces]"
但是如果我使用 qmf_t X_hybrid_left[32][32] = {{{0}}};
,即每边 3 个大括号,警告就会消失。
每侧使用三个支架是否正确?这是什么意思?
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}}};
来自 C11 规范,6.7.9 初始化 语法
initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }
尽管在您的特定情况下(将 2 个数组的所有对象归零),qmf_t X_hybrid_left[32][32] = {0};
的工作方式与 qmf_t X_hybrid_left[32][32] = {{{0}}};
相同,但编译器可能会警告您。
但是如果你想要任何非零初始化,你需要使用多个大括号。
来自同一部分:
[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.
[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. 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.