Ian Pennebaker
Ian Pennebaker

Reputation: 245

Incompatible types in assignment error in c

I am writing a game and currently working on an undo move. This should be very simple but I am getting this error and cant seem to figure it out. Here is my function...

bb_undo(BBoard board){
board->score = board->previousScore;
board->boardDim = board->previousBoard;
}

And here is the board structure...

struct bboard {
char boardDim[MAX_ROWS][MAX_COLS];
int score;
int rows;
int cols;
char previousBoard[MAX_ROWS][MAX_COLS];
int previousScore;
};

I should also probably add that bboard is a pointer. Anybody have any ideas? Thanks.

Upvotes: 2

Views: 263

Answers (2)

molbdnilo
molbdnilo

Reputation: 66371

Arrays can't be assigned to.

You'll need to either uses memcpy, or, since structs can be assigned to, you can define

struct board 
{
    char cells[MAX_ROWS][MAX_COLS];
};

and change bboard

struct bboard {
    board boardDim;
    int score;
    int rows;
    int cols;
    board previousBoard;
    int previousScore;
};

and then you can write

bboard b;
b.previousBoard = b.boardDim;

but you'll need

b.previousBoard.cells[x][y];

to access the elements.

Of course, you can add accessor functions to board to get rid of that annoyance:

struct board 
{
    char  operator() (size_t r, size_t c) const { return cells[r][c]; }
    char& operator() (size_t r, size_t c)       { return cells[r][c]; }
    char cells[MAX_ROWS][MAX_COLS];
};

so you can write

char value = b.previousBoard(x, y);

and

b.previousBoard(x, y) = 'x';

Upvotes: 3

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

Arrays have no the assignmnet operator. So instead of this statement

board->boardDim = board->previousBoard;

you have to copy arrays for example with using memcpy

Upvotes: 3

Related Questions