Comparison between integers and pointers?

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 
Hm, try single quotes.
Thanks, tummy!
Welcome.
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.
Thanks for the info, mulpro.
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
Topic archived. No new replies allowed.