I know that the correct way to compare "strings" in C is by using strcmp
, but now I tried comparing some character arrays with the ==
operator, and got some strange results.
Take a look at the following code:
int main()
{
char *s1 = "Andreas";
char *s2 = "Andreas";
char s3[] = "Andreas";
char s4[] = "Andreas";
char *s5 = "Hello";
printf("%d\n", s1 == s2); //1
printf("%d\n", s3 == s4); //0
printf("%d\n", s1 == s5); //0
}
The first printf
correctly prints a 1
, which signals that they are not equal. But can someone explain to me why, when comparing the character arrays, the ==
is returning a 0
?
Can someone please explain to me why the first printf
is returning a 1
(ie, they are equal) and the character arrays are returning a 0
?