问题描述
顾名思义,它会在缺少字段初始值设定项时触发。但它没有触发以下代码的任何警告。
#include <stdio.h>
struct test {
int a,b,c;
};
void func(struct test test) {
printf("%d,%d,%d\n",test.a,test.b,test.c);
}
int main() {
func((struct test) {12,.a = 1,12,.a = 13,.b = 13});
return 0;
}
当我运行 gcc test.c -Wmissing-field-initializers
时,它编译时没有警告。它打印出13,13,0
。这是 -Wmissing-field-initializers
的默认行为吗?
解决方法
此选项不对指定的初始化程序发出警告
试试
#include <stdio.h>
struct test {
int a,b,c;
};
void func(struct test test) {
printf("%d,%d,%d\n",test.a,test.b,test.c);
}
int main() {
func((struct test) {1,2}); // Now you get a warning
return 0;
}