Robert Allen
Robert Allen

Reputation: 152

C++ compare a byte to a letter

I have serial data I want to compare a specific byte to the letter "G".

LPtype is a byte that was read from the serial port.

if (LPtype == "G")
{
    doThis();
}

I get the following error:

C++ forbids comparison between pointer and integer

What would be the proper way of comparing the incoming bye to the letter G? (or any other letter for that matter)

Upvotes: 3

Views: 2466

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

You can do this:

  LPtype == 'G'

Upvotes: 1

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

LPtype == 'G'

Singular quotes. But LP usually prefixes pointer types, in which case you should dereference it

*LPtype == 'G'

But if you're sure LPtype is indeed a byte value, then

LPtype == 'G' 

should work. The thing is that "G" has type const char[2], and is not an integer type, whereas 'G' has type char and is integer type

Upvotes: 7

Related Questions