Search code examples
c++linuxgcccompiler-warningssuppress-warnings

Bool will never be NULL [-Waddress]


I'm starting in C++ and got this warning:

../src/loop.cpp:23:6: warning: the address of ‘bool almostComparison(double, double)’ will never be NULL [-Waddress]

Any can explain to me why this? If the intention is just a comparison for returning true or false?

I've been researching other questions, but the answers are too advanced for me to be able to understand what happened. Thanks for helping me :)

#include <iostream>

using namespace std;

bool almostComparison(double number1, double number2) {
    if (number1 + 1 == number2 || number2 + 1 == number1) {
        return true;
    }
    return false;
}

void numberComparison(double number1, double number2) {
    if(number1 < number2) {
        cout << "the smaller value is: " << number1 << '\n';
        cout << "the larger value is: " << number2 << '\n';
        if(almostComparison) {
            cout << "the numbers are almost equal!" << '\n';
        }
    } else if (number2 < number1) {
        cout << "the smaller value is: " << number2 << '\n';
        cout << "the larger value is: " << number1 << '\n';
    } else {
        cout << "the numbers are equal" << '\n';
        cout << "Number1: " << number1 << '\n';
        cout << "Number2: " << number2 << '\n';
    }
}

int main() {

    bool condition = true;
    double number1 = 0;
    double number2 = 0;

    while(condition) {
        cin >> number1;
        cin >> number2;

        if(cin.fail()) {
            condition = false;
        } else {

            numberComparison(number1, number2);

        }
    }

    return 0;
}

Solution

  • if(almostComparison) {
    

    This tests the address of the function, which will always be non-NULL. You want to actually call the function like so:

    if(almostComparison(number1, number2)) {