OK, so I haven't programmed in C for years and years. Haven't had a reason to until now, I'm helping out a friend with his first programming course in university - he's new to programming in general, and the first language they use is C. (Don't ask me why.)
Anyway, while browsing through his work, I thought I found a bug in his code. It's in the vein of:
Code:
if (stringvariable == "value") {
...which I've always thought was a big no-no (you'd just be comparing pointers, i.e memory addresses), and that you should use strcmp() in string.h for what you intend to do. But just to remind myself and double-check, I wrote the following test program and compiled it with GCC:
Code:
#include <stdio.h>
int main()
{
char* p = "abc";
char* s = "abc";
(p == s) ? printf("true") : printf("false");
return 0;
}
And hey, it printed "true". Huh. But what about:
Code:
#include <stdio.h>
int main()
{
char* p = "abc";
(p == "abc") ? printf("true") : printf("false");
return 0;
}
And what do you know, that also printed "true".
So has C changed since I last learned about it, or is this just evidence of a damn smart and helpful (at least in this situation) compiler?