如何修复警告:初始化程序周围缺少大括号?
Posted
技术标签:
【中文标题】如何修复警告:初始化程序周围缺少大括号?【英文标题】:How to repair warning: missing braces around initializer? 【发布时间】:2012-11-24 14:59:36 【问题描述】:警告是由vala生成的c代码产生的。
警告:初始化器周围缺少大括号
代码有效,但警告很烦人。警告引用的vala代码是
struct Position uint x; uint y;
private static Position positions[8];
生成的C代码是
static Position det_positions[8] = 0;
我尝试了六种不同的方式初始化位置,但似乎无法获得满足警告的语法。这是GCC bug 53119 还是有办法解决它?
【问题讨论】:
我没有在 GCC 4.6.3 上使用 Vala 0.16 或 0.18 收到此警告。愿意分享您的环境吗?另外,导致问题的生成 C 是什么? @apmasell 嵌入式 linux 目标是 Leopardboard 368,GCC 是 4.4.1,工具链是 codesourcery/arm-2010q1,我已将生成的 C 添加到问题中。我还正确格式化了我之前忘记做的代码。 【参考方案1】:是的,这似乎与GCC bug 53119 有关。如果您将 C 声明更改为 0
,它就会消失。您的选择是:
-
忽略警告。
使用
sed
等在生成后操作 C 代码以在该行上使用 0
而不是 0
。
在 Vala 中声明数组extern
,并在别处编写 C 定义。 (#2 的永久版本。)
执行struct foo int bar; Position positions[8]; static foo position_holder
和0
之类的操作将初始化position_holder.bar
,这很好,警告就会消失。
【讨论】:
或在编译中添加“-Wno-missing-braces”以消除无效警告。 但是请注意,警告通常不是无效的!仅对于零初始化(即= 0
),它总是无效的。有关警告的有效案例,请参阅 Adit Ya's answer。【参考方案2】:
当设置了 -Wall 编译器标志的多维数组被视为线性数组(尽管它仍然正确并且代码运行完美)时也会出现此警告。
例如
char array[5][10][2] = \
"0","0","0","0","0","0","0","0","0","0",\
"1","1","1","1","1","1","1","1","1","1",\
"2","2","2","2","2","2","2","2","2","2",\
"3","3","3","3","3","3","3","3","3","3",\
"4","4","4","4","4","4","4","4","4","4" ;
这将产生警告。
进行以下更改以删除如下所示的警告
char array[5][10][2] = \
"0","0","0","0","0","0","0","0","0","0" ,\
"1","1","1","1","1","1","1","1","1","1",\
"2","2","2","2","2","2","2","2","2","2",\
"3","3","3","3","3","3","3","3","3","3",\
"4","4","4","4","4","4","4","4","4","4" ;
如果我错了,请纠正我。
【讨论】:
在本例中,数组的第一级和第二级用
输入,第三级通过定义字符串(以'\0'
结尾)用"
输入。在 2D 中,如果您还使用字符串,则仅使用一级
:"0"
即char '0'
然后char '\0'
后跟零,如果您不使用字符串,则使用两级
: 0
即只有零。以上是关于如何修复警告:初始化程序周围缺少大括号?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 .format() 打印字符串,并在替换的字符串周围打印文字大括号 [重复]