我有:
char player_one[10];
printf("Enter name for first player:\n");
scanf("%s",player_one);
struct player
{
char name[MAX_NAME_LEN+1];
enum colour col;
};
在我的.h文件中声明,但当我尝试在.c文件中创建一个新播放器时。即 -
player p1 {player_one, blue}
编译器说错误:未知类型名称'player'
答案 0 :(得分:8)
struct
之前需要 player
个关键字。
struct player p1 {...,...}
或者您可以使用typedef来避免struct
关键字。
像
typedef struct
{
char name[MAX_NAME_LEN+1];
enum colour col;
}player;
和
player p1 {player_one, blue}
答案 1 :(得分:1)
要么像Jayesh回答一样放struct
,要么使用typedef
:
typedef struct player {...} player;