I'm trying to compare two char arrays. They appear to be identical, but the comparison fails and I cannot work out why. I'm using it to parse topics from the pubsubclient MQTT library.
I declare a global char array
char PWMTopic[64];
and later in the program, I assign a string to it. The string comes from an SD card.
if (!strcmp(tempBuf1, strParam[8]))
strcpy(PWMTopic, tempBuf2);
I print this to the monitor and it is exactly what I expect it to be, properly terminated, with no surprise leading or trailing characters.
When I subsequently compare it to a predetermined char array, they both print out identically, but strcmp fails.
if (!strcmp(topic, PWMTopic))
Serial.println("PWM Topic");
Comparing the two, they print out as shown below, the triangle brackets showing there are no hidden chars, making the two different. They are both the same length as well.
<occ/shed/po> - <occ/shed/po>
However, If I use
if (!strcmp(topic, "occ/shed/po"))
Serial.println("PWM Topic");
This succeeds.
I can provide the entire code, but it's spread across 6 files and I thought some bright person may be able to point out my mistake from this summary.
No pressure, but my sanity is on the edge
Thanks
"succeeds" in what way. Did you, perhaps, think that strcmp() returned 'true' on a match and 'false' on a mismatch? strcmp() returns 0 (false) when there is a match. !strcmp() is true when there is a match.
strcmp() returns -1 for "less than", 0 for "equal" and +1 for "greater than.
if (!strcmp(topic, "occ/shed/po"))
Serial.println("PWM Topic");
But this doesn't:
strcpy(PWMTopic, "occ/shed/po");
if (!strcmp(topic, PWMTopic))
Serial.println("PWM Topic");
It sounds like the contents of your buffer is not exactly what you think. I would display the characters in HEX before the comparison, just to be sure there are no invisible characters.
for (int i = 0; i < strlen(PWMTopic); i++)
{
Serial.print((byte)PWMTopic[i], HEX);
Serial.print(' ');
}
Serial.println();
if (!strcmp(topic, PWMTopic))
Serial.println("PWM Topic");
This file shows the problem. The test.cfg file consists of a single line of text
PWMTopic:occ/shed/po\n
I'm using a Teensy 4.1 board with a built in SD card, hence the reference to BUILTIN_SDCARD. Change this as required for whatever you use; I'm sure the teensy isn't responsible for the problem.