2

So if I have this line of code below, it returns the error also shown below.

string beads = "example";

if (beads[0] == "w")

./beads.cpp:19:15: error: comparison between pointer and integer ('int' and 'const char *')
    if (beads[0] == "w") {
        ~~~~~~~~ ^  ~~~

However, if I change the statement to the statement below, everything works well. Why is that?

if (beads[0] == *"w")
Vedaad Shakib
  • 739
  • 7
  • 20
  • 1
    Why do people down vote? Its been a plague lately. – ojblass May 22 '14 at 01:44
  • 1
    @ojblass, It *is* a rather RTFM type question tbh. – chris May 22 '14 at 01:45
  • 1
    I am not saying it is dissertation material. he provided code... he tried to help himself... he showed us what he tried. – ojblass May 22 '14 at 01:46
  • Thanks for all the informative answers! It fixed my problem. And sorry for my noobish question; I am pretty new to c++. I come from a Python background where " and ' do the same thing. – Vedaad Shakib May 22 '14 at 01:49
  • @ojblass We're trying to maintain a helpful Q&A site here, rather _'being helpful'_ for particular, localized, badly asked questions here! (I gave an answer myself though) – πάντα ῥεῖ May 22 '14 at 01:49
  • @ojblass advice from `meta` is that very basic questions should be downvoted. This is the sort of thing you'd learn in early chapters of a book, or early on in a C course. This site is about hosting questions that will have lasting value to the community . – M.M May 22 '14 at 04:24
  • Join one of the [meta](http://meta.stackoverflow.com/questions/254358/why-the-backlash-against-poor-questions/254361) [discussions](http://meta.stackoverflow.com/questions/255261/how-to-answer-extreme-beginner-questions/255335) if you'd like to voice your opinion on this topic, rather than via a question's comments. – M.M May 22 '14 at 04:37

5 Answers5

2

Since beads is an std::string, beads[0] is a char. Hence, you need to compare it to a 'w' (with single quotes), a char constant, and not to "w", a string literal.

Your other code snippet

if (beads[0] == *"w")

is correct, but it is inefficient and counterintuitive. Dereferencing a string literal is the same as taking its initial letter, so it's the same as

if (beads[0] == "w"[0])

It compiles, but it's inferior to

if (beads[0] == 'w')

where character literal 'w' is used.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

In C and C++, double quotes represent a string literal -- the string is null terminated and stored somewhere in memory.

"w" turns out to be some random address, which you can't determine.

As you've been told, use if (beads [0] == 'w'), which is a character literal (note the single quotes) -- a number, not a pointer.

smiling_nameless
  • 1,047
  • 1
  • 9
  • 23
1

You probably want

if (beads[0] == 'w')

This compares something to a character. *"*w" is pointing to some nonsensical location.

ojblass
  • 21,146
  • 22
  • 83
  • 132
  • `*"w"` is actually fine. `"w"` is a `const char [2]` which decays to `const char *` and dereferences to the first character of the string literal. It is, however, weird and not all that useful. – Zan Lynx May 22 '14 at 01:49
1

For character comparison, use ' instead of ". So you should use

if (beads[0] == 'w')

instead of

if (beads[0] == "w")
rcs
  • 6,713
  • 12
  • 53
  • 75
1

Fix your code as follows

if (beads[0] == 'w')
             // ^ ^ ** NOTE the single quotes 

You want to compare against a plain char value in this condition.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190