Home > Backend Development > C++ > Why Does My C Code Throw \'ISO C forbids comparison between pointer and integer\'?

Why Does My C Code Throw \'ISO C forbids comparison between pointer and integer\'?

Barbara Streisand
Release: 2024-10-29 11:20:02
Original
692 people have browsed it

 Why Does My C   Code Throw

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template