ISO C++ forbids comparison between pointer and integer [-fpermissive]_

I am not sure why I am getting this error in my code. It seems that my variable num is not being passed to my function printIt.

#include <stdio.h>

 void printIt(int *num);

int main(){
    int num;
    printf("What is your annual income? ");
    scanf("%d", &num);
    printIt(&num);

    return 0;
}

void printIt(int *num){

    if (num > 90000){
        printf("Congratulations! You are doing great.");
        return;
    }else{
        printf("You will make $90,000, if you keep going.");
        return;
    }
}

2 answers

  • answered 2020-01-14 01:54 Jerry Coffin

    You've defined printIt as receiving a pointer to an int, so inside of it, num is the address of that int, and *num is the value of the int.

    IMO, it would be better to change it to void printIt(int num); and when you call it, just pass num directly: printIt(num);. There's no real point in passing the address in this case.

  • answered 2020-01-14 01:59 Simson

    In your function printIt num is a pointer to an integer and *num is the income you have been given from the user.

    There are two ways of solving this either change the signature of printIt or change the code.

    Alternative one

    void printIt(int num){
        if (num > 90000){
            printf("Congratulations! You are doing great.");
            return;
        }else{
            printf("You will make $90,000, if you keep going.");
            return;
        }
    }
    

    and then call it with printIt(num); instead.

    Alternative two: change the code and keep the function call as it is

    void printIt(int *num){
        if (*num > 90000){
            printf("Congratulations! You are doing great.");
            return;
        }else{
            printf("You will make $90,000, if you keep going.");
            return;
        }
    }