Compiler Error: Pointer and Integer Comparison Prohibition in C
The compiler issue "ISO C forbids comparison between pointer and integer" arises during compilation, highlighting a code error relating to improper comparison between a pointer and an integer. To delve into the resolution, let's examine the provided code:
In the given code, the error occurs within the if statement where answer is being tested against "y":
<code class="cpp">if (answer == "y") ...</code>
Here, answer is a character variable, whereas "y" is a C-style string literal. Attempting to compare a character variable to a pointer to a character (as "y" translates to a pointer) triggers the compiler error.
You have two potential solutions to address this issue:
1. Use String Variable:
The preferred solution is to employ a string variable for answer instead of a character variable:
<code class="cpp">string answer;</code>
This aligns with best practices and avoids the pointer-integer comparison error.
2. Use Character Constant:
Alternatively, you can utilize a character constant with single quotes, which represents a character rather than a string:
<code class="cpp">if (answer == 'y') ...</code>
Both solutions effectively resolve the pointer-integer comparison prohibition, ensuring correct code execution.
The above is the detailed content of Why Does My C Code Throw \'ISO C forbids comparison between pointer and integer\'?. For more information, please follow other related articles on the PHP Chinese website!