C++

Error: ISO Forbids Comparison Between Pointer and Integer

This article is about an error that commonly occurs while comparing pointers and integers. Before moving on to our topic, we should know what ISO is. ISO stands for International Standards Organization. This company develops and publishes international standards. While programming in C++ Language, we often get this error that ISO forbids comparison between Pointer and Integer. This error also occurs in other languages such as C Language. The main question arises on why ISO does not allow comparisons between pointers and integers.

The answer is that in C++ Language when a programmer tries to compare two different datatypes, the system will throw the comparison error. So, an integer is a different datatype and a pointer is an address of memory. Therefore, the compiler will give an error because they both are different datatypes. This error not only occurs while comparing pointers and integers, it occurs whenever we try to compare two variables of different datatypes.

Syntax

It is an error message so it does not have any syntax but we can show you what this error looks like:

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

Example # 01:

Now, to understand the error in depth we will perform an example in which we will compare our variable with a different datatype and we will see whether the system throws any error or not. For that purpose, we have declared a character array named “x” with a length of 5. We have printed our messages for user readability. Now, if we take a look at line number 4, we will see that we are comparing our variable with the word ‘Hello’. The purpose of our code is if the values of our variable and passed value match. The compiler should go inside the if condition. After that, the compiler will return(0) and successfully exit from our code. Now, let us execute our code.

#include <iostream>

using namespace std;
char x[5];
cout<> x;
if (x == 'Hello')
{
cout<<"Hi How are You";
}
return(0);
}

After executing our code, we will get the following error from our compiler as a result. If we look closely at the error message, we can see that the compiler has pointed out an error in line 7. This is the line where we were trying to compare our variable with a value “Hello”. The compiler takes strings or characters in double quotes like (“Hello”). But here we have passed the value in single quotes like (‘Hello’). So, the datatypes do not match and the compiler recognizes our passed value as a pointer. Because of all that, the compiler will not be able to do a comparison between them and it will throw an error which we can see in the figure below in red color.

Now, let us try to resolve the error and successfully execute our code. For that purpose, in our case, we will only have to change the passed value from ‘Hello’ to “Hello” so that the system recognizes it as a character. We will simply change single quotes with double quotes. After that, we will execute our code.

After executing our code, the above figure is what our output will look like. The compiler has successfully executed our code and printed what we asked it to print. After that, it shows us the message that Program ended with exit code 0 because at last, we returned 0 from our main function. So, we can see that by only passing the correct data type, we have resolved our issue.

Example # 02:

In the previous example, we explained how the compiler throws an error. But we did not take an integer as our variable. We will take an integer and will try to compare it with the pointer. To do so, we have initialized an array “i” with 1 length. We have declared another variable r which is an integer and passed it the value 4. We want to see if the values match the system should return 1 and if they are not equal then the system will return 0. So, let us execute our code to see what happens.

#include <stdio.h>

#include <iostream>

using namespace std;
int main ()
{
    int i[1];
    int r = 4;
    {
        if(i != r)
        {
            return (0);
        }
        else{
            return (1);
        }
    }
}

After the execution of our code, this is the output. The system has given us the error once again. In this case, it is on line 10. If we go back and look at our code, we will see that on line 10, we are comparing our array with our integer. We have not specified what index are we comparing. So, the system will compare the whole memory with our integer. In our code, the memory will be a pointer. The system will give us the error because of this inconsistent datatype.

Now, we will resolve the error. To do so, we will simply tell the length of our array to the system. For that, we will replace the “i” with “i[1]” so that the system will take the integer from the index of our array and will compare that integer with our custom declared integer that is “r”.

Now in the above image, we can see that our code is successfully executed and returned to us 0. This means that i[1] is not equal to r. But the main thing is that the error is resolved and the code is successfully compiled and executed. If the values were the same the system would have returned 1.

Conclusion

In this guide, we discussed an error that occurs while writing code. We explained why this error occurs, what mistakes a programmer makes which led to this error, and how to avoid and resolve the error. We also performed different examples and tried to get this error in different conditions. After that, we resolved the errors and pointed out the mistakes in our code to help you understand better.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.