user3925467
user3925467

Reputation: 53

The correct way to use struct

I have:

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;
};

declared in my .h file, but when I try to create a new player in the .c file. ie -

player p1 {player_one, blue}

the compiler says error: unknown type name 'player'

Upvotes: 0

Views: 83

Answers (2)

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

struct keyword required before player.

struct player p1 {...,...}

or you can use typedef for avoiding struct keyword.

like

typedef struct 
{
    char name[MAX_NAME_LEN+1];
    enum colour col;
}player;

and

player p1 {player_one, blue}

Upvotes: 8

NoDataFound
NoDataFound

Reputation: 11949

Either put struct before like Jayesh answer, or use a typedef:

typedef struct player {...} player;

Upvotes: 1

Related Questions