使用struct的正确方法

时间:2014-08-16 09:13:30

标签: c struct

我有:

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'

2 个答案:

答案 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;