我正在学习如何使用结构来尝试制作纸牌游戏。我一直在搞这个事情大概是永远的,我无法让printf语句按照我的想法工作。我认为这与cc2
没有正确地分配到ctwo.typ
有关,但我真的不知道该怎么做。尝试从结构体打印字符串时运行失败
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char typ[20];
int num;
} card;
int main(void)
{
char cc2[] = "Two of Clubs";
card ctwo;
ctwo.typ[20] = *cc2;
ctwo.num = 2;
//The output of the following is "Two of Clubs"
printf("%s\n", cc2);
//The output of the following is "2, "
printf("%i, %s\n", ctwo.num, ctwo.typ);
//The output of the following is "2, (null)"
printf("%i, %s\n", ctwo.num, ctwo.typ[0]);
return 0;
}
此行:ctwo.typ [20] = * cc2; 是非常错误的。想想那条线。 –
是的,思考........这是你应该花一些时间,而不是随机尝试的地方。 :) – 0decimal0
太局部化是这类问题的一个有用的密切原因。这不是一个无效的问题,但它并没有为任何其他人提供任何惊人的洞察力 - 它只是混淆搜索。哦,好吧... –