0

My code got an error below: 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.

3
  • 1
    Why are you passing enum cell * token to the function when all you do with it is use it like a local variable? Commented Aug 11, 2016 at 5:07
  • How is init_first_player()called, what are you passing and how are those values defined and initialised? Commented Aug 11, 2016 at 5:11
  • Do you know the difference between a pointer and a thing that is not a pointer? Commented Aug 11, 2016 at 6:28

2 Answers 2

2

You are passing token in as a pointer, presumably because you want to see the modified value at the call site. (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 = *token;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much
1

In init_first_player token is a pointer to enum

In your structure first, token is an enum.

You cannot assign a pointer to enum to an enum.

You should use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.