[英]Create Array of Strings Inside of Struct
I'm trying to create an array of strings inside a struct which represents a player's inventory. 我正在尝试在结构中创建一个字符串数组,表示玩家的库存。
I create a struct for the player: 我为玩家创建了一个结构:
typedef struct Player {
char *inventory[];
} Player;
And then I use a function which allocates heap memory and creates (not sure if "create" is the right word here) the struct with some "items" inside the player's "inventory". 然后我使用一个分配堆内存的函数,并创建(不确定“create”是否为正确的单词)结构,在玩家的“库存”中包含一些“项目”。
Player *spawnPlayer(void)
{
Player *stats = malloc(sizeof(Player));
stats->inventory[] = {"potion", "potion", "ether"};
return stats;
}
Now I can create a normal array like this outside of a struct, but if I attempt to use the above, I get the below error while trying to compile: 现在我可以在struct之外创建一个这样的普通数组,但如果我尝试使用上面的代码,我在尝试编译时会遇到以下错误:
arrays.c: In function 'spawnPlayer':
arrays.c:13:19: error: expected expression before ']' token
stats->inventory[] = {"potion", "potion", "ether"};
Would someone be able to point me in the right path as to why this doesn't work? 有人能够指出我正确的道路,为什么这不起作用?
Your definition of Player
is an example of a C99 extension call flexible array: an array of unknown size that is the last member of a structure and that will be accessible only upto the size actually allocated for each instance. 您对
Player
定义是C99扩展调用灵活数组的一个示例:一个未知大小的数组,它是结构的最后一个成员,只能访问实际为每个实例分配的大小。 You probably did not mean to use that. 你可能并不想使用它。 And you cannot initialize the values with the syntax used in your
spawnPlayer
function. 并且您无法使用
spawnPlayer
函数中使用的语法初始化值。
You can define an array of fixed size this way: 您可以通过以下方式定义固定大小的数组:
typedef struct Player {
char *inventory[3];
} Player;
And you can initialize an allocated instance of Player
this way: 您可以这样初始化
Player
已分配实例:
Player *spawnPlayer(void) {
Player *stats = malloc(sizeof(*stats));
stats->inventory[0] = "potion";
stats->inventory[1] = "potion";
stats->inventory[2] = "ether";
return stats;
}
If you meant for the array to have a size known at runtime and use a flexible array, you probably want to add a member for the actual size allocated: 如果您希望数组在运行时具有已知大小并使用灵活数组,则可能需要为分配的实际大小添加成员:
typedef struct Player {
int size;
char *inventory[];
} Player;
And you will allocate it and initialize it this way: 您将分配它并以这种方式初始化它:
Player *spawnPlayer(void) {
Player *stats = malloc(sizeof(*stats) + 3 * sizeof(*stats->inventory));
stats->size = 3;
stats->inventory[0] = "potion";
stats->inventory[1] = "potion";
stats->inventory[2] = "ether";
return stats;
}
Flexible arrays are a C99 extension, you can simulate them in C90 by defining inventory
with a size of 0
is the compiler supports it, or a size of 1
, but it is not strictly portable. 灵活的数组是C99扩展,您可以通过定义大小为
0
inventory
来编译C90,编译器支持它,或者大小为1
,但它不是严格可移植的。
There is a third possibility, using a pointer to an array of char*
: 还有第三种可能性,使用指向
char*
数组的指针:
typedef struct Player {
int size;
char **inventory;
} Player;
Player *spawnPlayer(void) {
Player *stats = malloc(sizeof(*stats));
stats->size = 3;
stats->inventory = malloc(sizeof(*stats->inventory) * 3);
stats->inventory[0] = "potion";
stats->inventory[1] = "potion";
stats->inventory[2] = "ether";
return stats;
}
typedef struct Player {
char *inventory[10];
} Player;
static const char *arr[] = { "potion", "potion", "ether"};
memcpy(stats->inventory, arr, sizeof(arr));
Or 要么
typedef struct Player {
char **inventory;
} Player;
Player *stats = malloc(sizeof(Player));
stats->inventory = (char**)malloc(sizeof(char*) * 10);
static const char *arr[] = { "potion", "potion", "ether"};
memcpy(stats->inventory, arr, sizeof(arr));
Or 要么
stats->inventory[0] = "potion";
stats->inventory[1] = "potion";
stats->inventory[2] = "ether";
Perhaps you should add a NULL pointer at the end to indicate the length. 也许你应该在末尾添加一个NULL指针来指示长度。
stats->inventory[3] = NULL;
static const char *arr[] = { "potion", "potion", "ether", NULL };
memcpy(stats->inventory, arr, sizeof(arr));
Or simply have a int inventory_size
in your struct. 或者只是在struct中有一个
int inventory_size
。
You need to explicitly declare an array called inventory specifying its size. 您需要显式声明一个名为inventory的数组,指定其大小。
Then initialize a pointer to point to it. 然后初始化指针指向它。
This is the third time I've seen this mistake in two days. 这是我第二次在两天内看到这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.