What I would like to do here is initializing all the players' names with the empty string value ""
, I have heard it can be done at the declaration point in C99, but I'm not entirely sure how.
我想在这里做的是用空字符串值“”初始化所有玩家的名字,我听说它可以在C99的声明点完成,但我不完全确定如何。
typedef struct data_players
{ char name[25];
int hp;
int mana;
int id;
}player;
player player_list[10]
In this case how should I proceed?
在这种情况下,我该怎么办?
2 个解决方案
#1
2
At declaration time, you initialize structs with {}
like this:
在声明时,您使用{}初始化结构,如下所示:
player player_list[10] = {{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0}};
Note that any skipped initializers will default to zero, so if you are always initializing the entire array to zero, this will suffice:
请注意,任何跳过的初始值设定项都将默认为零,因此如果您始终将整个数组初始化为零,这就足够了:
player player_list[10] = {0};
Also note that if player_list
is a global or a static variable, it will automatically be initialized to all zeroes. As if you called
另请注意,如果player_list是全局变量或静态变量,它将自动初始化为全零。好像你打过电话
memset(player_list, 0, sizeof(player_list));
at program start.
在节目开始。
#2
3
struct data_players
{ char name[25];
} player = "";
or
struct data_players
{ char name[25];
} player = { "" };
This initializes the first member of the structure at declaration time. If there are more members, separate them with comma. One more thing. Your struct should not be type-defined at declaration time to do that kind of initialization.
这会在声明时初始化结构的第一个成员。如果有更多成员,请用逗号分隔。还有一件事。在声明时,您的结构不应该是类型定义的,以进行这种初始化。
if you have an array of structures and you want to initialize them in C99 you can use "designated initializer" at declaration time.
如果你有一个结构数组,并且你想在C99中初始化它们,你可以在声明时使用“指定的初始化程序”。
player arr [2] = { {.name = ""}, {.name = ""} };
This will initialize arr[0]
and arr[1]
's name members to a null string. Though I see not much sense in it as it defaults to a null string anyway.
这会将arr [0]和arr [1]的名称成员初始化为空字符串。虽然我觉得它没有多大意义,因为它无论如何都默认为空字符串。
#1
2
At declaration time, you initialize structs with {}
like this:
在声明时,您使用{}初始化结构,如下所示:
player player_list[10] = {{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0},{"", 0, 0, 0}};
Note that any skipped initializers will default to zero, so if you are always initializing the entire array to zero, this will suffice:
请注意,任何跳过的初始值设定项都将默认为零,因此如果您始终将整个数组初始化为零,这就足够了:
player player_list[10] = {0};
Also note that if player_list
is a global or a static variable, it will automatically be initialized to all zeroes. As if you called
另请注意,如果player_list是全局变量或静态变量,它将自动初始化为全零。好像你打过电话
memset(player_list, 0, sizeof(player_list));
at program start.
在节目开始。
#2
3
struct data_players
{ char name[25];
} player = "";
or
struct data_players
{ char name[25];
} player = { "" };
This initializes the first member of the structure at declaration time. If there are more members, separate them with comma. One more thing. Your struct should not be type-defined at declaration time to do that kind of initialization.
这会在声明时初始化结构的第一个成员。如果有更多成员,请用逗号分隔。还有一件事。在声明时,您的结构不应该是类型定义的,以进行这种初始化。
if you have an array of structures and you want to initialize them in C99 you can use "designated initializer" at declaration time.
如果你有一个结构数组,并且你想在C99中初始化它们,你可以在声明时使用“指定的初始化程序”。
player arr [2] = { {.name = ""}, {.name = ""} };
This will initialize arr[0]
and arr[1]
's name members to a null string. Though I see not much sense in it as it defaults to a null string anyway.
这会将arr [0]和arr [1]的名称成员初始化为空字符串。虽然我觉得它没有多大意义,因为它无论如何都默认为空字符串。