[英]C: initializing struct with an array of strings
I'm trying to do the following, but the compiler is complaining about brackets, however, I can't find my way to an alternative. 我正在尝试执行以下操作,但编译器正在抱怨括号,但是,我无法找到替代方法。
struct cards {
char faces[13][6], suits[4][9];
}
typedef struct cards cards;
void init_struct(cards *s) {
s->suits = {"hearts","spades","clubs","diamonds"};
s->faces = {"ace","two","three","four","five",
"six","seven","eight","nine"
"ten","jack","queen","king"};
}
I realize that there are several possible duplicate threads out there, but none of them has led me on the track. 我意识到有几个可能的重复线程,但没有一个让我在轨道上。 I hope one of you can :) Thanks 我希望你们其中一个可以:)谢谢
#include <string.h>
typedef struct cards {
char faces[13][6], suits[4][9];
} cards;
cards base_card = {
{"ace","two","three","four","five",
"six","seven","eight","nine", //forgot "," at nine after
"ten","jack","queen","king"},
{"hearts","spades","clubs","diamonds"}
};
void init_struct(cards *s) {
memcpy(s, &base_card,sizeof(cards));
}
The direct initialization syntax can only be used for initialization, not assignment. 直接初始化语法只能用于初始化,而不能用于赋值。 You cannot do this, for example: 你不能这样做,例如:
char p[2][5];
p = {"a", "b"}; //error
That's why it fails to compile. 这就是它无法编译的原因。 Try strcpy
-ing string by string 尝试strcpy
-ing string by string
strcpy(s->suits[0], "hearts");
strcpy(s->suits[1], "spades");
...etc
or, alternatively, initialize a temporary array and then copy it 或者,初始化临时数组然后复制它
char suits_tmp[4][9] = {"hearts","spades","clubs","diamonds"};
memcpy(s->suits, suits_tmp, 4*9);
Use const char *
within your struct
(I assume there is no requirement to modify the actual content of the suit/face value) and initialise them individually: 在你的struct
使用const char *
(我假设不需要修改套装/面值的实际内容)并单独初始化它们:
struct cards {
const char *suits[4];
const char *faces[13];
};
typedef struct cards cards;
void init_struct(cards *s)
{
s->suits[0] = "hearts";
s->suits[1] = "spades";
s->suits[2] = "clubs";
s->suits[3] = "diamonds";
s->faces[0] = "ace";
s->faces[1] = "two";
s->faces[2] = "three";
s->faces[3] = "four";
s->faces[4] = "five";
s->faces[5] = "six";
s->faces[6] = "seven";
s->faces[7] = "eight";
s->faces[8] = "nine";
s->faces[9] = "ten";
s->faces[10] = "jack";
s->faces[11] = "queen";
s->faces[12] = "king";
}
Of course, if you just want a one-off set of cards, which is reasonable, then this will work: 当然,如果你只想要一套一卡通,这是合理的,那么这将有效:
struct
{
const char *suits[4];
const char *faces[13];
} cards =
{
{"hearts","spades","clubs","diamonds"},
{"ace","two","three","four","five",
"six","seven","eight","nine",
"ten","jack","queen","king"}
};
#include <string.h>
#include <stdio.h>
struct cards {
const char** suits;
const char** faces;
};
typedef struct cards cards;
const char* suits[4] = {"hearts","spades","clubs","diamonds"};
const char* faces[13] = {"ace","two","three","four","five",
"six","seven","eight","nine"
"ten","jack","queen","king"};
int main()
{
cards deck;
deck.suits = suits;
deck.faces = faces;
printf(deck.suits[0]);
return 0;
}
This works as well. 这也有效。 Uses no pointers. 不使用指针。
Clarification 澄清
I know mine is the quick and dirty answer, but there is no strcpy
or memcpy
or a long list of assignments. 我知道我的答案是快速而肮脏的,但没有strcpy
或memcpy
或很长的作业列表。 If your plan is to use the standard deck of cards for your game, then it would be a constant set of values anyway. 如果你的计划是为你的游戏使用标准牌组,那么无论如何它将是一组恒定的值。 If your intent is to have different types of decks, then my answer may not be adequate. 如果您打算使用不同类型的套牌,那么我的答案可能不够充分。 Yes, it doesn't have a init_struct
function, but you could easily modify it for your intent (since I am not well versed in C and malloc.) 是的,它没有init_struct
函数,但您可以根据自己的意图轻松修改它(因为我不熟悉C和malloc。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.