Compiler Error: ISO C 's Prohibition of Pointer-Integer Comparisons
While experimenting with a simple function from Bjarne Stroustrup's C textbook, developers frequently encounter the compile error: "ISO C forbids comparison between pointer and integer." This error stems from the comparison between a pointer and an integer.
One instance of this issue arises when comparing a character input to the string "y". In the provided code:
<code class="cpp">#include <iostream> #include <string> using namespace std; bool accept() { cout << "Do you want to proceed (y or n)?\n"; char answer; cin >> answer; if (answer == "y") return true; return false; }</code>
The error occurs because the code defines answer as a character (char), while "y" is a string literal. To resolve this, you have two options:
Both solutions address the compiler's restriction by ensuring that you are comparing a pointer to an integer with a pointer to another integer or a pointer to a string.
The above is the detailed content of Why Does C Forbid Comparing Pointers and Integers?. For more information, please follow other related articles on the PHP Chinese website!