简体   繁体   中英

How to explain and usage of “array in struct”?

typedef struct A
{
     short B;
     short C;
} New_Type;

struct Move_Information
{
     New_Type Position [25];
};

I am a newbie in C, and I don't really understand the meaning of "array in struct".

Could any wizard explain how to use it ? Thanks.

It simply means that one member in a struct, Position in your case, is an array. In this case it's an array of the type New_Type , which happens to be a struct too, but that doesn't matter.

You can access indexed elements of the array just as with any other array:

struct Move_Information moves;
moves.Position[0].B = 12;
moves.Position[0].C = 4711;

Suppose you had a plain old c type in a struct:

struct Other_Information
{
    int x[25];
};

You could then make one of these structs and access the data member as follows:

Other_Information info;
info.x[0] = 42;//set the first item

Similarly, for Move_Information you can index into the array, then access that structures members a so:

Move_Information info;
info.Position[0].B = 42;

It is nothing but declaring a structure which contains array of type New_type .

to use it -

Struct Move_Information new_node ;

new_node.position[x].B =  "your B data ";
new_node.position[x].C =  "your C data ";

Hope it clarifies your doubt .

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