Operand types are incompatible ("char" and "const char*")

53,398

Double quotes are the shortcut syntax for a c-string in C++. If you want to compare a single character, you must use single quotes instead. You can simply change your code to this:

char userInput_Text[3];

if (userInput_Text[1] == 'y') { // <-- Single quotes here.
    // Do stuff.
}

For reference:

  • "x" = const char *
  • 'x' = char
Share:
53,398
LeviTheDegu
Author by

LeviTheDegu

Updated on January 27, 2020

Comments

  • LeviTheDegu
    LeviTheDegu over 4 years

    I'm receiving the following error...

    Operand types are incompatible ("char" and "const char*")

    ... when trying to perform an if statement. I'm assuming I'm not understanding how the input value is stored although I'm unsure if I can just cast it into the matching type?

    Example code to reproduce is:

    char userInput_Text[3];
    
    if (userInput_Text[1] == "y") {
        // Do stuff.
    }
    

    I'm not sure what's causing this. It would appear that one type is a char and the other is a const char pointer although I'm unsure of what, for reference this error also occurs when I'm not using an array).

    And tips / feedback would be much appreciated.

  • Karl Nicoll
    Karl Nicoll over 11 years
    @LeviTheDegu Speaking as someone whose has developed his fair share of code in languages where ' and " mean the same thing, I understand your frustration :-)
  • chris
    chris over 11 years
    Technically, "x" is a const char (&)[2], not a const char *.
  • Karl Nicoll
    Karl Nicoll over 11 years
    Very true @chris, but I thought it better to use const char * in relevance to the question. Comment upvote though :)