Your first method using ==
is comparing the pointers, so it will only return true if the two are exactly the same char array, meaning they point to the same piece of memory. The other method using strcmp
is the way you would do this, because it is comparing the actual content of the strings. So it can return true even if the two strings are in different locations in memory.
The reason your first method is appearing to work correctly is because the two variables are pointing to the same string literal, so they basically point to the same location in memory when they're the same string, and different locations when they're different strings. If you used malloc
to allocate memory for those strings, and set their contents, then they would point to different locations in memory, so your first method would return false
and the second would return true
for the same string of text.