枚举与enum *的枚举不兼容

时间:2016-08-11 04:59:16

标签: c pointers enums

我的代码出现以下错误: incompatible types when assigning to type enum cell from type enum cell *

我尝试了很多方法来解决它但没有奏效。这是我的代码:

BOOLEAN init_first_player(struct player * first, enum cell * token) {
    strcpy(first->name, "Bob");
    first->score = 0;

    int colorNo = rand() % 2;
    token = (colorNo + 1 == 1) ? RED : BLUE;

    first->token = token; //Error occurs here
    return TRUE;
}

this is my data structure:

struct Player {
    char name[20];
    enum cell token; //takes 0 - 1 - 2
    unsigned score;
};

enum cell {
    BLANK, RED, BLUE
};

有人可以修改代码,因为我不知道我做错了什么。

2 个答案:

答案 0 :(得分:2)

您将token作为指针传递,可能是因为您希望在调用网站上看到修改后的值。 (如果您不关心修改后的值,则根本不应该在此函数中发送它。)

因此,您需要在分配时取消引用它:

// use *token (instead of just token) to dereference and assign
*token = (colorNo + 1 == 1) ? RED : BLUE;

将其分配给first->token

时相同
first->token = *token;

答案 1 :(得分:1)

init_first_player token中是指向枚举的指针

在您的结构first中,token是一个枚举。

您无法将枚举指针指定给枚举。

你应该使用

*token = (colorNo + 1 == 1) ? RED : BLUE;
first->token = *token