简体   繁体   English

从enum到enum的分配不兼容*

[英]Incompatible assignment to enum from enum *

My code got an error below: incompatible types when assigning to type enum cell from type enum cell * 我的代码出现以下错误: incompatible types when assigning to type enum cell from type enum cell *

I have tried many ways to fix it but didnt work. 我已经尝试了多种方法来修复它,但没有成功。 This is my code: 这是我的代码:

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

Someone can please fix the code as I don't know what I have been doing wrong. 有人可以修复代码,因为我不知道自己在做什么错。

You are passing token in as a pointer, presumably because you want to see the modified value at the call site . 您将token作为指针传递进来, 大概是因为您想在调用站点上看到修改后的值 (If you don't care about the modified value, you shouldn't send it in this function at all). (如果您不关心修改后的值,则根本不要在此函数中发送它)。

So, you need to dereference it when assigning: 因此,分配时需要取消引用:

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

Same when assigning it to first->token : 将其分配给first->token时相同:

first->token = *token;

In init_first_player token is a pointer to enum init_first_player token是指向枚举的指针

In your structure first , token is an enum. 在你的结构firsttoken是一个枚举。

You cannot assign a pointer to enum to an enum. 您不能将指向枚举的指针分配给枚举。

You should use 你应该用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM