Comparison between integers and pointers?

Mar 9, 2010 at 1:13am
This is just supposed to see if what the second character of what the user enters is "x".
1
2
3
4
string c;
cout << "";
cin >> c;
if (c[1]=="x") //error here 

Output says:

41 C:\Users\Jim\Documents\test.cpp ISO C++ forbids comparison between pointer and integer 
Mar 9, 2010 at 1:16am
Hm, try single quotes.
Mar 9, 2010 at 1:19am
Thanks, tummy!
Mar 9, 2010 at 1:22am
Welcome.
Mar 9, 2010 at 1:27am
Hm, try single quotes.


Right, "x" is a string literal, which the compiler sees as an array of characters, or 'x' and 0 or '/0'.
So you're comparing a single character constant to a temporary multi-character array. Single quotes indicate single character constants.
Mar 9, 2010 at 1:32am
Thanks for the info, mulpro.
Mar 9, 2010 at 1:48am
NP, it's also worth noting that the compiler converts both character literals like 'x' and '\0' and string literals like "hello" to their ascii numerical equivalents. So the compiler warning you of a comparison between an integer and pointer shows you how the compiler sees the expression c[1] == "x", it converts the string character element c[1] to an integer, and the string literal "x" to a pointer to two integer values (an array), 120 and 0.
Last edited on Mar 9, 2010 at 1:49am
Topic archived. No new replies allowed.