Paul the Pirate
Paul the Pirate

Reputation: 767

Array declaration and initialization with structs

I am trying to declare an array of structs, is it possible to initialize all array entries to a default struct value?

For example if my struct is something like

           typedef struct node
           {    int data;
                struct node* next;
           }node;

Is there a way to declare data to 4 and next to null? What about 0 and null?

Upvotes: 0

Views: 110

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476950

Sure:

node x[4] = { {0, NULL}, {1, NULL}, {2, NULL}, {3, NULL} };

Even this should be fine:

node y[4] = { {0, y + 1}, {1, y + 2}, {2, y + 3}, {3, NULL} };

Upvotes: 3

Related Questions