gpt4 book ai didi

c - 尝试使用两个初始化列表初始化二维结构数组时发出警告

转载 作者:太空狗 更新时间:2023-10-29 14:53:15 43 4
gpt4 key购买 nike

我正在尝试使用以下行将用户定义类型的二维数组初始化为零,

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.

关于c - 尝试使用两个初始化列表初始化二维结构数组时发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749842/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com