Is strcmp "the" Arduino way?

I just answered in another thread suggesting OP there use strcmp to compare an incoming string (small-s) ala Robin2's Serial tut thread, to some target string "ON" to turn on an led.

That got me thinking, dangerous as that may be.

In Robin2's examples, where receivedChars[numChars] is a string, it works to say:

Serial.print(receivedChars);

So somehow here in Arduinoland, Serial knows to print all the elements of receivedChars but it doesn't work to say:

if (receivedChars == "ON");

That comparison seems only to work for chars not strings.

strcmp is of course much easier than doing an element-by-element comparison as Martyn Currey did here:

if (receivedChars[0] == 'O'  && receivedChars[1] == 'N' )

... where one would hope never to need to check for supercalifragilisticexpialidocious.

So my question: is strcmp the way to compare strings in Arduino C :wink: errr I mean an Arduino sketch?

Most of the cases, yes. Some cases you can just compare each element, that what strcmp() is doing under the hood anyway.

It's not an Arduino thing, it's a C thing.

What may help get your head round it is that in your example above, this:

receivedChars

is shorthand for:

&receivedChars[0]

So receivedChars is a pointer. Serial.print knows to do what a pointer to some characters - print them one by one until you come to a terminating \0.

You can compare two pointers but here:

if (receivedChars == "ON")

You're checking to see if two pointers (addresses) are the same. Clearly they're not because that string and the receivedChars array occupy different areas of RAM, which is why you get the initially counterintuitive result that you need to use strcmp.

wildbill:
It's not an Arduino thing, it's a C thing.

Yes I knew that; it's actually what prompted my question. Sub-text was, "is there an Arduino thing to do that...".

wildbill:
You're checking to see if two pointers (addresses) are the same. Clearly they're not because that string and the receivedChars array occupy different areas of RAM, which is why you get the initially counterintuitive result that you need to use strcmp.

Grrrrr, you used the "p-word"...Wash your mouth out with psoap.

Ok cool, that makes sense, so question answered, thanks both of you.