when does a const char * not equal what it looks like it equals?

06 Aug 2011

I'm using HTTPClient to grab a text file off a web server that is just a simple string as the contents. I'm then saving it to a variable so I can do something when it's a certain value.

Problem is, even when the value is what i want, the code things it's not. it's been years since I did any C++ and I'm not just following where I've failed.

txt is a HTTPText object, and lastState is a const char *

lastState = txt.gets(); printf("Got last state as: %s, %d \r\n", lastState, strlen(lastState)); if (lastState == "closed") { printf("looks like closed \r\n"); } else { printf("not closed \r\n"); } }

even when it prints "closed" and a length of 6, the test in the if statement fails. I've also tried strcmp and it also fails the test. If i comment out the gets() and set it to closed manually, the test passes. what am i doing wrong?

06 Aug 2011

Your

if (lastState == "closed")

statement is comparing the value of the pointers (ie. the address of where the two strings are allocated in memory) and not the contents of the strings themselves. Something like this should work better:

if (0 == strcmp(lastState, "closed"))
06 Aug 2011

I did try that, as mentioned. compiled again with some more prints and it worked this time

not the first time it's done that to me, seems occasionally it downloads a bin that isn't the current code. I'm saving to my desktop, then cut and paste to the mbed drive, so i know I'm getting a fresh file.