Why do I get “warning: missing initializer for member”? [-Wmissing-field-initializers]

后端 未结 5 2094
名媛妹妹
名媛妹妹 2021-01-04 20:14

I\'m wondering why I am getting a warning about initialization in one case, but not the other. The code is in a C++ source file, and using GCC 4.7 with -std=c++11

5条回答
  •  萌比男神i
    2021-01-04 20:52

    The only way to prevent that warning ( or error, if you or your organization is treating warning as error (-Werror option)) is to memset() it to init value. For example:

    #include 
    #include 
    
    typedef struct {
        int a;
        int b;
        char c[12];
    } testtype_t;
    
    int main(int argc, char* argv[]) {
        testtype_t type1;
    
        memset(&type1, 0, sizeof(testtype_t));
    
        printf("%d, %s, %d\n", argc, argv[0], type1.a);
        return 0;
    }
    

    Not very clean, however, seems like that for GCC maintainers, there is only one way to initialize a struct and code beauty is not on top of their list.

提交回复
热议问题