[英]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];
};
我是C語言的新手,我不太了解“結構體數組”的含義。
任何向導都可以解釋如何使用它嗎? 謝謝。
它僅表示結構中的一個成員(在您的情況下為Position
是一個數組。 在這種情況下,它是類型為New_Type
的數組,它也恰好是一個struct
,但這無關緊要。
您可以像訪問其他數組一樣訪問數組的索引元素:
struct Move_Information moves;
moves.Position[0].B = 12;
moves.Position[0].C = 4711;
假設您在結構中有一個普通的舊c類型:
struct Other_Information
{
int x[25];
};
然后,您可以使這些結構之一並按如下所示訪問數據成員:
Other_Information info;
info.x[0] = 42;//set the first item
同樣,對於Move_Information,您可以索引到數組中,然后按以下方式訪問該結構成員:
Move_Information info;
info.Position[0].B = 42;
只是聲明一個包含類型為New_type的數組的結構而已。
使用它-
結構Move_Information new_node;
new_node.position[x].B = "your B data ";
new_node.position[x].C = "your C data ";
希望它能澄清您的疑問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.