Search code examples
cdata-structuresgcc-warning

How do I initialize all fields of a structure when one of the fields is an array?


#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?


Solution

  • Try this:

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