【问题标题】:When does -Wmissing-field-initializers triggers warning?-Wmissing-field-initializers 何时触发警告?
【发布时间】:2021-04-27 16:15:06
【问题描述】:

顾名思义,它会在缺少字段初始值设定项时触发。但它没有触发以下代码的任何警告。

#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 的默认行为吗?

【问题讨论】:

    标签: c gcc gcc-warning


    【解决方案1】:

    来自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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-02
      • 2014-03-06
      • 2018-05-23
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2019-08-31
      相关资源
      最近更新 更多