Question

#include <stdio.h>

typedef struct
{
    int as;
    int bs;
    int cs;
}asd_t;

typedef struct
{
    asd_t asd[10];
}asd_field_t;    

typedef struct
{
    int a;
    int b;
    asd_field_t  asd_field[10];
}abc_t;    

int main()
{
    abc_t abc ={0,1,{0}};
    return 0;
}

In the above code, I am trying to initialize the structure abc_t. Compiling the above code as:

gcc -Wall sample.c

gives me:

sample.c: In function 'main':
sample.c:26: warning: missing braces around initializer
sample.c:26: warning: (near initialization for 'abc.asd_field[0].asd')

How do I avoid this warning?

Was it helpful?

Solution 2

Try this:

abc_t abc ={0,1,{{{{0,0,0}}}}};

OTHER TIPS

Struct abc_t has another struct inside of type asd_field_t which you initialize to 0 using {0}. The warning you're getting from GCC is because you're zeroing all members of that struct (asd_field), rather than filling them one by one. There is an argument that this behaviour by GCC is incorrect, given that the standard deems it to be perfectly valid to zero an entire struct by using {0}. You can read GCC's bug report here

You could also disable the annoying warning by passing the option -Wno-missing-braces so you get all the other wall warnings, ie: gcc -Wall -Wno-missing-braces test.c -o test

Well, you have to nested arrays in your struct abc_t, therefore, you have to do something like:

abc_t abc = {0,1,{{0,0,0}}};

abc_t abc ={0,1,{ {{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}},{{{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}} }};

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top