0

I cannot declare a address to the function pointer that I created earlier and I cannot understand what am i doing wrong.

I declared function pointer to the function that returns a bool and takes 1 integer argument.

Depending on the case, function pointer should point to the one of the two functions: bool is_black or bool is_white. There is no error when assigning pointer to the address of is_black function, but when I try to do the same thing with is_white, I get this error.

By trying to to the same thing in the very next function ('void cover_fields'), I don't get any errors.

bool is_white(int piece)
{
    if (piece > 0)
        return true;
    return false;
}
bool is_black(int piece)
{
    if (piece < 0)
        return true;
    return false;
}
bool is_mate(int** board, bool is_white=true)
{
    bool(*is_enemy)(int enemy_piece); //<-- HERE I DECLARED FUNCTION POINTER
    int KING;

    if (is_white)
    {
        is_enemy = &is_black;
        KING = WHITE_KING;
    }
    else
    {
        is_enemy = &is_white; //<-- ERROR ON THIS LINE
        KING = BLACK_KING;
    }

    short int col, row, king_col, king_row;

    //REST OF THE FUNCTION BELOW
    //finding king coordinates
    for (row = 0; row < 8; row++)
    {
        for (col = 0; col < 8; col++)
        {
            short int piece = board[row][col];
            if (piece == KING)
            {
                king_col = col;
                king_row = row;
                break;
            }
        }
    }
    
    if (!is_checked(board, false))
        return false;

    //If there is at least one free field that king can move to, return false
    //UP
    row = king_row - 1; col = king_col;
    if (field_is_valid(row , col) && 
        (board, row, col, false) 
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    // UP - RIGHT
    row = king_row - 1; col = king_col + 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //RIGHT
    row = king_row; col = king_col + 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //RIGHT-DOWN
    row = king_row+1; col = king_col + 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //DOWN
    row = king_row + 1; col = king_col;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //LEFT-DOWN
    row = king_row + 1; col = king_col - 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //LEFT
    row = king_row; col = king_col - 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //LEFT-UP
    row = king_row - 1; col = king_col - 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    return true;
}

// FUNCTION IN WHICH THERE IS NO ERROR EVEN THOUGH I DID THE SAME THING
void cover_fields(int** board, int row, int col, int row_dir, int col_dir,int moves, int piece_is_white)
/* Function covers all the fields on the board in certain direction
   direction is declared with row_dir, col_dir values that can be -1,0,+1
 */
{
    // Pointer to the function that is going to call the next move
    bool(*is_enemy_piece)(int enemy_piece); // DECLARING FUNCTION POINTER

    int PIECE = board[row][col];
    if (piece_is_white)
        is_enemy_piece = &is_black;
    else
        is_enemy_piece = &is_white; // <-- NO ERROR ON THIS LINE

    //REST OF THE FUNCTION
    short int _row = row; short int _col = col;
    /* _row & _col are temp values for row and col.
    *  they are getting increased every while itteration
        by the direction value: row_dir, col_dir
    */

    while (field_is_valid(_row + row_dir, _col + col_dir) &&
        (board[_row + row_dir][_col + col_dir] == EMPTY_FIELD ||
            is_enemy_piece(board[_row + row_dir][_col + col_dir])))
    {
        next_move(board, row, col, _row + row_dir, _col + col_dir, PIECE, piece_is_white, moves);
        _row += row_dir; _col += col_dir;
    }
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
kims9
  • 45
  • 1
  • 4
  • What does the second operand in parentheses if (field_is_valid(row , col) && (board, row, col, false) mean? The value of the second expression is always equal to false. It seems you forgot to prefix the parentheses with a function name.:) – Vlad from Moscow Sep 06 '21 at 19:37
  • Any modern IDE should be able do go to a definition and know which `is_white` definition is used in a fraction of a second... Otherwise by using find or by renaming a variable, you can easily fix that kind of problem by yourself. – Phil1970 Sep 06 '21 at 19:49
  • 1
    This doesn’t address the question, but instead of using booleans and function pointers, create an `enum` for the piece color. An enemy piece is one whose color is not equal to the king’s color. – Pete Becker Sep 06 '21 at 21:24
  • 1
    This doesn’t address the question, but `if (piece > 0) return true; return false;` can be written more simply as `return piece > 0;`. – Pete Becker Sep 06 '21 at 21:27

1 Answers1

4

You have a parameter to is_mate named is_white. This local variable will shadow the global function of the same name (any references to is_white will be to the parameter, not the function).

Solutions include changing the name of the parameter to something distinct (e.g., enemy_is_white), or fully qualifying the name of the function:

    is_enemy = &::is_white;
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56