Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a Struct of a Struct

Tags:

c

If I have a struct in C that has an integer and an array, how do I initialize the integer to 0 and the first element of the array to 0, if the struct is a member another struct so that for every instance of the other struct the integer and the array has those initialized values?

like image 943
Nathan Yeung Avatar asked Dec 11 '22 21:12

Nathan Yeung


1 Answers

Initialisers can be nested for nested structs, e.g.

typedef struct {
    int j;
} Foo;

typedef struct {
    int i;
    Foo f;
} Bar;

Bar b = { 0, { 0 } };
like image 66
Paul R Avatar answered Dec 27 '22 16:12

Paul R