Fix ISO C++ Warning: Comprehensive Guide on Resolving 'Forbids Comparison Between Pointer and Integer -fpermissive' Error

---
title: Fix ISO C++ Warning: Comprehensive Guide on Resolving 'Forbids Comparison Between Pointer and Integer -fpermissive' Error
author: Your Name
date: 2021-12-31
---

  

In this guide, we will walk you through the process of resolving the common ISO C++ warning, which states the comparison between pointer and integer is forbidden. This error occurs when you try to compare a pointer with an integer value in your C++ code, resulting in a compilation error. The error message is as follows:

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


To fix this error, follow the step-by-step instructions below.

## Step 1: Locate the problematic code

First, you need to locate the line of code causing the error. The error message should provide the file name and line number where the issue is present. Open the file in your favorite text editor or IDE and navigate to the specified line number.

## Step 2: Examine the code

Next, examine the code and try to identify the pointer and integer being compared. The comparison might be inside an `if` statement, a `while` loop, or a `for` loop. Look for any variables that are defined as pointers (e.g., `int *ptr`) and are being compared with integer values (e.g., `0`, `1`, `NULL`, etc.).

## Step 3: Correct the code

To fix the error, you need to ensure that the comparison is being made between the correct data types. There are two possible solutions:

### Solution 1: Compare the pointer with a valid pointer

Change the integer value to a valid pointer value. For example, if you are comparing a pointer to `NULL`, replace it with `nullptr` (C++11 and later) or the correct pointer value.

```cpp
int *ptr;
if (ptr != nullptr) {
    // Your code here
}

Solution 2: Dereference the pointer

Dereference the pointer before making a comparison. This will allow you to compare the actual value stored at the memory location pointed to by the pointer with the integer value.

int *ptr;
if (*ptr != 0) {
    // Your code here
}

Step 4: Recompile your code

After making the necessary changes, recompile your code to see if the error has been resolved. If the error persists, repeat the steps above to locate and fix any additional instances of the issue.

FAQ

Why does ISO C++ forbid comparison between pointer and integer?

The comparison between pointer and integer is forbidden in ISO C++ because it can lead to undefined behavior and potential security issues. Comparing a pointer with an integer value can result in incorrect and unpredictable results, making it difficult to debug and maintain the code.

What is the -fpermissive flag?

The -fpermissive flag is a compiler option in GCC that allows non-conforming code to compile by downgrading certain diagnostics from errors to warnings. This flag can be helpful in cases where you are working with legacy code or third-party libraries that do not strictly adhere to the C++ standard.

Can I use the -fpermissive flag to ignore this error?

While it is possible to use the -fpermissive flag to ignore this error, it is not recommended. Ignoring the error and allowing the code to compile can lead to undefined behavior and potential security issues. Instead, it is best to fix the error by following the steps outlined in this guide.

What is the difference between NULL and nullptr?

NULL is a macro that represents a null pointer constant, which is an integer value of zero. nullptr, introduced in C++11, is a keyword that represents a null pointer value of any type. The use of nullptr is preferred over NULL because it provides better type safety and is more expressive.

How can I prevent this error in the future?

To prevent this error in the future, make sure to always compare pointers with valid pointer values or dereference the pointer before making a comparison with an integer value. Additionally, consider using modern C++ features like nullptr and smart pointers (e.g., std::unique_ptr, std::shared_ptr) to improve the safety and readability of your code.

Related Links

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.