我有:
char *card[4];
gets(&card[player - 1]); // 2 chars at a time (e.g. 2S, 3D, TF)
我可以通过以下方式打印字符串:
printf("%s",&card[0]);
但是如何从&card[0]
中获取每个字符(例如'2'
和'S'
)?
答案 0 :(得分:0)
只需将它们编入索引。例如:
struct PlayerView: View {
var body: some View {
VStack{
....
}.navigationBarTitle(Text(“-2:49”).foregroundColor(.blue))
}
}
但是,您的示例代码是错误的(没有分配内存并占用了调用printf("%c %c", card[0][0], card[0][1]);
的地址)。
相反,请执行以下操作:
gets
这为5个玩家保留了空间,每个玩家都有一个可以容纳3个字符的缓冲区。总共15个字节。
检查#define NPLAYERS 5
#define LENGTH 3 // 2 + the null terminator
char card[NPLAYERS][LENGTH];
int player = 0; // first player
fgets(card[player], LENGTH, stdin);
printf("%c %c", card[player][0], card[player][1]);
的返回码是个好主意。
答案 1 :(得分:0)
您可能想做的事情如下:
char card[3];
fgets(card, 3, stdin);
printf("%c %c", card[0], card[1]);