-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) {12,.a = 1,12,.a = 13,.b = 13});
    return 0;
}

当我运行 gcc test.c -Wmissing-field-initializers 时,它编译时没有警告。它打印出13,13,0。这是 -Wmissing-field-initializers 的默认行为吗?

解决方法

来自documentation

此选项不对指定的初始化程序发出警告

试试

#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;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...