我已经开始用 C 语言做纸牌游戏,但我找不到任何东西来代替 string
,这显然在 C 语言中没有使用。下面是一些代码,这成为了一个障碍:
struct cards {
string index;
string colour;
};
void load_cards(cards deck[MAX_C])
{
int i = 0;
for (int j = 0; j < 4; j++) {
deck[i].index = '2';
deck[i += 1].index = "3";
deck[i += 1].index = "4";
deck[i += 1].index = "5";
deck[i += 1].index = "6";
deck[i += 1].index = "7";
deck[i += 1].index = "8";
deck[i += 1].index = "9";
deck[i += 1].index = "10";
deck[i += 1].index = "W";
deck[i += 1].index = "D";
deck[i += 1].index = "K";
deck[i += 1].index = "A";
}
for (int j = 0; j < 13; j++) {
deck[j].colour = "T";
}
for (int j = 13; j < 26; j++) {
deck[j].colour = "k";
}
for (int j = 26; j < 39; j++) {
deck[j].colour = "K";
}
for (int j = 39; j < 52; j++) {
deck[j].colour = "P";
}
for (int j = 52; j < 56; j++) {
deck[j].colour = "JR";
}
}
最佳答案
string
实际上是 C++ 中的一个类,正如其他人指出的那样,它是 char 数组/指针的包装器(可用于存储chars
)序列>).
在 C 中,您必须手动使用字符数组。您可以通过两种方式(主要):
- 创建一个静态字符数组(具有足够的大小)。
- 使用
malloc
动态创建字符数组。但在这种情况下,当不再需要该数组时,您还必须手动释放它。
当然,由于您使用的是 C,因此您必须显式/手动处理数组溢出条件、重叠、空终止等。
关于c++ - 我可以在 C 中使用什么来代替 C++ 中的 "string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135224/