我正在制作一款 Roguelike 游戏。我想将 map 表示为一个结构数组,例如一个数组中有 256 个结构。 map 是一个 16*16 的方 block 网格,每个方 block 都有属性,比如上面是否有元素。
假设我想要一个包含 256 个结构 tiles
的数组:
struct tiles {
char type; /* e.g. dirt, door, wall, etc... */
char item; /* item on top of it, if any */
char enty; /* entity on top of it, e.g. player, orc if any */
}
然后,我需要访问这样的结构数组:
int main(void)
{
unsigned short int i;
struct tiles[256];
for (i = 1; i <= 256; i++) {
struct tiles[i].type = stuff;
struct tiles[i].item = morestuff;
struct tiles[i].enty = evenmorestuff;
}
}
最佳答案
要声明一个 struct tiles
数组,只需将它放在变量之前,就像处理其他类型一样。对于 10 个 int
int arr[10];
类似地,声明一个包含 256 个 struct tiles
的数组
struct tiles arr[256];
要访问 arr
元素的任何成员,例如 type
,您需要 .
运算符作为 arr[i].type
关于c - 如何在C中制作一个结构数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32185751/