简体   繁体   中英

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 ?

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} };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM