C++ comparing a char to a string literal [duplicat

2019-08-13 15:40发布

问题:

This question already has an answer here:

  • c++ compile error: ISO C++ forbids comparison between pointer and integer 5 answers

Beginning programmer here...

I'm writing a very simply program for my computer science class and I ran into an issue that I'd like to know more about. Here is my code:

#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    char courseLevel;

    cout << "Will you be taking graduate or undergraduate level courses (enter 'U'"
            " for undergraduate,'G' for graduate.";
    cin >> courseLevel;

    if (courseLevel == "U")
    {
        cout << "You selected undergraduate level courses.";
    }

    return 0;
}

I'm getting two error messages for my if statement: 1) Result of comparison against a string literal is unspecified (use strncmp instead). 2) Comparison between pointer and integer ('int' and 'const char*').

I seem to have resolved the issue by enclosing my U in single quotes, or the program at least works anyway. But, as I stated, I'd simply like to understand why I was getting the error so I can get a better understanding of what I'm doing.

回答1:

You need to use single quotes instead.

In C, (and many other languages) a character constant is a single character1 contained in single quotes:

'U'

While a string literal is any number of characters contained in double quotes:

"U"

You declared courseLevel as a single character: char courseLevel; So you can only compare that to another single char.

When you do if (courseLevel == "U"), the left side is a char, while the right side is a const char* -- a pointer to the first char in that string literal. Your compiler is telling you this:

Comparison between pointer and integer ('int' and 'const char*')


So your options are:

if (courseLevel == 'U')       // compare char to char

Or, for sake of example:

if (courseLevel == "U"[0])    // compare char to first char in string

  1. Note for completeness: You can have mulit-character constants:

    int a = 'abcd'; // 0x61626364 in GCC

But this is certainly not what you're looking for.



回答2:

Rapptz is right, but I think some more elaboration should help...

courseLevel == "U"

In C and C++, double-quotes create string literals - which are arrays of characters finishing with a numerical-0 ASCII-NUL terminating sentinel character so programs can work out where the text ends. So, you basically are asking if a character is equal to an array of characters... they just can't be compared. Similar questions that are valid are:

  • does this character variable hold a specific character value: courseLevel == 'U'
  • does this character variable appear in a specific array: strchr(courseLevel, "U")
  • does this character variable match the first element in a specific array: courseLevel == "U"[0]

Of course, the first one of these is the one that makes intuitive sense in your program.



回答3:

The reason why you get an error is because string literals in C and C++ end with a null terminated character \0 while single characters don't. So when you compare to a char to a string literal you're comparing the character literal to a char array {'U','\0'}.



标签: c++ char compare