Hello
Error:Code:int main() { string str; str = "01"; if(str[0] == "0") cout << "Equal"; }
ISO C++ forbids comparison between pointer and integer
How does one make it so that str[0] refers to the actual character with a string datatype to it?
Hello
Error:Code:int main() { string str; str = "01"; if(str[0] == "0") cout << "Equal"; }
ISO C++ forbids comparison between pointer and integer
How does one make it so that str[0] refers to the actual character with a string datatype to it?
"0" is a C-style string, not a char. Use single quotes for char - '0'.
This error message is a bit misleading. But it makes perfect sense. Here's what happens:
[0] fetches a specific element of the string, a character, which is of type char. The char is promoted to an integer before being compared.
"0" is a C-style string, an array of two chars, which decays to a pointer const char*.
So in the end, you try to compare an int with a const char* and that is why the compiler complains the way it does.