Thread: The address will always evaluate as true warning

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    The address will always evaluate as true warning

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    #define M 5
    #define N 5
    
    
    // 2 - free spot ; -1 - vertical line ; -2 - horizontal line ; 1 - X ; 0 - 0
    
    
    int board[M][N];
    
    
    
    
    void printBoard(int board[M][N])
    {
        int i,j;
    
    
        for(i = 0; i < 5; i=i+2)
        {
            for(j = 1; j <= 3; j = j+2)
            {
                board[i][j] = -1;
            }
        }
    
    
        for(j = 0; j < 5; j++)
        {
            board[1][j] = -2;
            board[3][j] = -2;
        }
    
    
        for(i = 0; i <= 4; i=i+2)
        {
            for(j= 0; j <= 4; j=j+2)
            {
                board[i][j] = 2;
            }
        }
    
    
        for(i = 0; i < 5; i++)
        {
            for(j = 0; j < 5; j++)
            {
                if(board[i][j] == -1)
                {
                    putchar('|');
                }
                else if(board[i][j] == 2)
                {
                    putchar(' ');
                }
                else if(board[i][j] == -2)
                {
                    putchar('-');
                }
                else if(board[i][j] == 1)
                {
                    putchar('X');
                }
                else if(board[i][j] == 0)
                {
                    putchar('0');
                }
            }
            putchar('\n');
        }
    }
    
    
    
    
    bool isEmpty(int board[M][N], int i, int j)
    {
        if(board[i][j] == 2)
            return true;
        else
            return false;
    }
    
    
    void place_X(int board[M][N], int i, int j)
    {
        if(isEmpty(board,i,j))
            board[i][j] = 1;
        else
            printf("The block has already been used!\n");
    }
    
    
    void place_zero(int board[M][N], int i, int j)
    {
        if(isEmpty(board,i,j))
            board[i][j] = 0;
        else
            printf("The block has already been used!\n");
    }
    
    
    bool checkBoardFull(int board [M][N])
    {
        int i,j;
    
    
        for(i = 0; i < 5; i++)
        {
            for(j = 0; j < 5; j++)
            {
                if(isEmpty(board, i, j))
                    return false;
            }
        }
    
    
        return true;
    }
    
    
    
    
    int main(int argc, char **argv)
    {
        bool running = true;
        char user_1[20], user_2[20];
        int i,j;
    
    
        printf("Enter the name of player 1 (X):\n");
        scanf("%19s", user_1);
        printf("Enter the name of player 2 (0):\n");
        scanf("%19s", user_2);
        printf("\n\n");
        printf("\t\t\t||------------------------------------||\n");
        printf("\t\t\t||------------------------------------||\n");
        printf("\t\t\t||                                    ||\n");
        printf("\t\t\t||                                    ||\n");
        printf("\t\t\t||                                    ||\n");
        printf("\t\t\t||          %s   vs   %s          ||\n", user_1, user_2);
        printf("\t\t\t||                                    ||\n");
        printf("\t\t\t||                                    ||\n");
        printf("\t\t\t||                                    ||\n");
        printf("\t\t\t||------------------------------------||\n");
        printf("\t\t\t||------------------------------------||\n");
        printf("\n\n");
    
    
        printBoard(board);
    
    
        while(running)
        {
            if(!checkBoardFull)
            {
                printf("Pick i position:\n");
                scanf("%d", &i);
                printf("Pick j position:\n");
                scanf("%d", &j);
            
                place_X(board, i , j);
                printBoard(board);
            }
            running = false;
            printBoard(board);
        }
        return 0;
    }
    I'm trying to create a tic tac toe game, but I'm getting a strange warning:
    warning: the address of ‘checkBoardFull’ will always evaluate as ‘true’ [-Waddress]
    137 | if(!checkBoardFull)
    Any idea why?

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    12
    You forgot the argument list on that function call. Change line 157 to:
    Code:
                   if (!checkBoardFull(board))
    That should get you farther along in your testing.

    PS: The reason for that warning is that a C converts function name alone (without a parenthesized argument list) to a pointer to the function. That is usually implemented as the memory address of the function at runtime, and any non-NULL pointer evaluates as "true" when used in a "boolean context" (if or while conditions, or anywhere else that a "true/false" test is needed.)
    Last edited by husoski; 05-13-2021 at 03:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-28-2012, 07:29 PM
  2. warning: address of local variable ‘f’ returned
    By dayalsoap in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2011, 10:51 PM
  3. Replies: 4
    Last Post: 07-11-2011, 10:50 AM
  4. What is the true address of a function ptr?
    By Raigne in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2008, 12:24 PM
  5. Address will always return true? (class vectors + member vars)
    By littleweseth in forum C++ Programming
    Replies: 12
    Last Post: 11-27-2003, 01:49 AM

Tags for this Thread