That_One _Gamer652
That_One _Gamer652

Reputation: 1

Error ISO C++ forbids comparison between pointer and integer

I got this error:

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

My code:

#include <stdio.h>

int main()
{
    int i[1];
    
    int r = 4;
    
    {
        printf("enter a number between 1-10\n");
        while (i != r);
        {
        scanf("%d,&i[0]");
        }
        printf("good job\n :)");
    }
}

Upvotes: 0

Views: 2244

Answers (1)

user12002570
user12002570

Reputation: 1

The problem is that the variable i in your code above, is an array of int which decays to a pointer to an int due to type decay. On the other hand, the variable r is an int. So when you wrote:

while (i != r)

this means you're trying to compare a pointer to an int with an int and hence the said error.

To solve this you can use the following program:


#include <iostream>

int main()
{
    int arr[4] = {}; //create an array named arr of size 4 with elements of type int all initialized to 0
    
    //iterate through the array and take input from user 
    for(int &element : arr)
    {
        std::cout << "Enter number:" << std::endl;
        std::cin >> element; 
    }
    
    return 0;
}


Upvotes: 2

Related Questions