C++

ERROR: ISO C++ forbids comparison between pointer and integer

In C++, single apostrophes are used to represent characters, not strings. We utilize double quotes symbols to epitomize the pointer. After compiling the programs in C++, we get different errors. Comparison error is one of them. Sometimes after running the program, we acquire “ISO C++ forbids comparison between pointer and integer” or from time to time, we obtain “ISO C++ forbids comparison between pointer and integer [-fpermissive]”. When we get these types of errors, we have to check the comparison condition applied to the program. We must keep this thing in mind that the comparison condition is incompatible with the distinct data type. For solving C++ prohibits comparison error, we simply have to evaluate the comparable form of defined variables in conditions.

In this artifact, we are working on these errors and see how to resolve them in C++.

Having different data types of variables during comparison:

After using the namespace standard, we declare the function in this instance. Then we initialize the variable ‘v’ for value and assign the value 1. We apply the if condition to compare the value with function. If the value is greater than the function, there will be an increment by 1 in the value. The function has to return the value. Now we start the main body of the code. We declare the variable ‘v’.

#include <iostream>
using namespace std;
int f(int j, int k) {
    int v = 1;
    if (v > f) {
      return (v+1);
    }
    return v;
}
 
int main() {
  int v = f(4,7);
  return 0;
}

After compiling, we obtain the error ‘ISO C++ forbids comparison between pointer and integer [-fpermissive]’, so the program is not executed.

When we apply the condition if (v>f), we make a mistake here. We compare the function to integer, so here this comparison is wrong. Always compare those variables which contain a similar data type. When an assessment is inaccurate, we’ll acquire this type of error. Hence right here, we have to evaluate with a few integral values. Here we’ve eliminated the function call and evaluated it with an integer value. By doing this, the error might be resolved.

Invalid type ‘double (int)’:

At the beginning of the program, we encompass the library <stdio.h>. Then we define the size of the list. In the main body, we define the variables. The list data type is ‘double’. We declare the function ‘get_avg’ having data type double. We pass two parameters for the function. Next, we declare the ‘sum’ variable with the double data type. We apply for loop, and the value is incremented to get the sum.

#include <stdio.h>
#define SIZE 15

int main(void){
double list;
double get_avg(const double list[], int r);
int j;
double sum = 0;
int n[SIZE];
 for (j = 0; j<r; ++j){
   sum += list [j];
  }
return(sum);
}

Here we also get the same error. Because the function always contains the arguments with the same data type. And in this code, we pass the parameter of the function having different data types.

Use if-else statement:

This program first includes the header file <iostream> for input and output purposes. Next, we use the namespace standard. In the main body of the code, we declare the string ‘s’. The data type of the string is ‘char’. We pass value 5 as an argument to the string. Further, we apply the if-else condition; if the user enters ‘xyz’, the ‘cout’ function displays ‘correct’ on the screen; otherwise, it displays an ‘In correct’ message.

#include <iostream>
using namespace std;
int main() {
    char s[5];
    cout << "Enter string";
    cin >> s;
    if (s == 'xyz')
    {
         cout << "correct";
    } else {
        cout << "In correct";
    }
    return 0;
}

When we execute the program, we acquire the error: ‘ISO C++ forbids comparison between pointer and integer [-fpermissive]. If (s==’xyz’), here ‘s’ is constant char* type. And that is an array of characters. But here, ‘xyz’ is deliberate as an integer value. It must be an insistent string value due to the fact that a single quotation mark is deliberated as an integral value.

We get different types of errors after running the programs. The errors are of two kinds’ run-time error and compiler-time error. This error is a run-time error.

How to solve the error?

Hereafter integrating the header file and using namespace standard. We do further coding in the main body. The string is declared. We use ‘cout’ to print the message ‘Enter the string’ on the screen, so the user enters the string. To check the condition, we use the if-else statement.

#include <iostream>
using namespace std;
int main() {
    string s;
    cout << "Enter the string: ";
    cin >> s;
    if (s == "xyz")
    {
         cout << "correct";
    } else {
        cout << "In correct";
    }
    return 0;
}

Hence for comparison, we have to utilize double quotation marks “xyz”. And state it as a variable of string. Now we can relate by using s == “xyz”.

As the user inputs the ‘xyz’ string, the program prints correctly because we defined the same string in the above code. The user entered a string, and the defined string is the same, so we get this output.

Conclusion:

This article first defines the error ISO C++ forbids comparison between pointer and integer. We also explained the reason behind this error. And the method to resolve this issue. When we compare the variables having, unlike data types, we acquire this type of error. To eliminate this type of error, we have to utilize variables with identical data types when comparing two variables.

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.