C array initialization at end of struct

时间:2016-04-04 18:55:08

标签: c 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?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

Yes. They are same. It is similar to

int i = 1;  

and

int i;
i = 1;