简体   繁体   中英

C array initialization at end of struct

I'm learning C and I am confused about this stuct I came across, but I think maybe it is short hand for simply creating a struct array.

struct myStruct
{
    char *name;
    int id;
} myList[] = {
  {"bob", 1},
  {"joe", 2}
};

Is the same as

struct myStruct
{
    char *name;
    int id;
};
struct myStruct myList[]  = {
  {"bob", 1},
  {"joe", 2}
};

Or am I wrong?

Yes, it is the same. The first syntax is useful in situations when you would like to keep the type of your struct anonymous:

struct {
    char *name;
    int id;
} myList[] = {
    {"bob", 1},
    {"joe", 2}
};

Yes. They are same. It is similar to

int i = 1;  

and

int i;
i = 1;

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