Comparing character arrays with the == operator in C

I know that the correct way to compare "strings" in C is to use strcmp , but now I tried to compare some character arrays with the == operator and got some weird 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, comparing arrays of characters, == returns 0 ?

Can someone explain to me why the first printf returns 1 (i.e. they are equal), and character arrays return 0 ?

+4
source share
7 answers

The == parameter compares the memory address.
Your compiler probably makes s1 and s2 point to the same static data to save space.

T. "Andreas" in the first two lines of code is stored in your executable data. The C standard says that these strings are constant and thus optimized two pointers to the same storage.

The char [] lines create a variable by copying the data into the variable and therefore are stored elsewhere on the stack at run time.

+20
source

Uh ... when == prints 1, that means they are equal. It differs from strcmp, which returns the relative order of strings.

+4
source

You are comparing addresses, not strings. The first two are permanent and will be created only once.

 int main() { char *s1 = "Andreas"; char *s2 = "Andreas"; char s3[] = "Andreas"; char s4[] = "Andreas"; char *s5 = "Hello"; printf("%d\n", s1 == s2); //1 printf("%p == %p\n", s1, s2); printf("%d\n", s3 == s4); //0 printf("%p != %p\n", s3, s4); printf("%d\n", s1 == s5); //0 printf("%p != %p\n", s1, s5); } 

The output is on my computer, but you get the idea:

 1 0x1fd8 == 0x1fd8 0 0xbffff8fc != 0xbffff8f4 0 0x1fd8 != 0x1fe0 
+2
source

Wait a second ... 1 means true, 0 means false. Therefore, your explanation is partially opposite. As for why the first two lines seem to be equal: the compiler built this constant line (s1 / 2) only once.

+1
source

s1 == s2 means " (char*) == (char*) " or that the addresses are the same.

The same goes for s3 == s4 . That " arrays decay into pointers " at work.

And you misunderstand the result of the comparison:

 0 == 0; /* true; 1 */ 42 == 0; /* false; 0 */ "foo" == "bar"; /* false (the addresses are different); 0 */ 
+1
source

All values ​​from s1 to s5 are not char ; they point to char . So, you are comparing the memory addresses of each line, not the lines themselves.

If you display the addresses in this way, you can see what the comparison operators actually work for:

 #include <stdio.h> int main() { char *s1 = "Andreas"; char *s2 = "Andreas"; char s3[] = "Andreas"; char s4[] = "Andreas"; char *s5 = "Hello"; printf("%p\n", s1); // 0x80484d0 printf("%p\n", s2); // 0x80484d0 printf("%p\n", s3); // 0xbfef9280 printf("%p\n", s4); // 0xbfef9278 printf("%p\n", s5); // 0x80484d8 } 

Exactly where the lines are allocated in memory is implementation-specific. In this case, s1 and s2 point to the same static memory block, but I would not expect this behavior to be portable.

+1
source

You cannot compare strings, but you can compare pointers.

0
source

All Articles