简体   繁体   English

如何解释和使用“结构体数组”?

[英]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". 我是C语言的新手,我不太了解“结构体数组”的含义。

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. 它仅表示结构中的一个成员(在您的情况下为Position是一个数组。 In this case it's an array of the type New_Type , which happens to be a struct too, but that doesn't matter. 在这种情况下,它是类型为New_Type的数组,它也恰好是一个struct ,但这无关紧要。

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: 假设您在结构中有一个普通的旧c类型:

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,您可以索引到数组中,然后按以下方式访问该结构成员:

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

It is nothing but declaring a structure which contains array of type New_type . 只是声明一个包含类型为New_type的数组的结构而已。

to use it - 使用它-

Struct Move_Information new_node ; 结构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 . 希望它能澄清您的疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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