Strcmp () and strcat ()

On my way to hone my C skills with C Pointers literature, I came across this code. In this problem I have to justify the conclusion. I am familiar with strcat() and strcmp() . I know that strcmp() returns 0 when two lines are passed, the same.

 # include <stdio.h> # include <string.h> int main() { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; int l; l = strcmp(strcat(str3, strcpy(str2, str1)), strcat(str3, "good")); printf("%d\n", l); return 0; } 

The response indicates 0, which means that the two calculated lines must be the same. I tried to solve this problem in several steps.

First tried strcat(str3, strcpy(str2, str1)) . 'str2' changes to 'Ok,' then strcat() changes str3 to 'DayGood.' My gcc compiler agrees with me so far.

Going to strcat(str3, "good") , since str3 has already been changed to DayGood , strcat changes str3 to DayGoodgood .

Again, gcc is with me.

 int main() { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; int l; printf("%s\n", strcat(str3, strcpy(str2, str1))); printf("%s\n", strcat(str3, "good")); //l = strcmp(strcat(str3, strcpy(str2, str1)), strcat(str3, "good")); //printf("%d\n", l); return 0; } 

He produces

 DayGood DayGoodgood 

I tried this variation again.

 int main() { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; int l; printf("%s\n", strcat(str3, "good")); printf("%s\n", strcat(str3, strcpy(str2, str1))); //l = strcmp(strcat(str3, strcpy(str2, str1)), strcat(str3, "good")); //printf("%d\n", l); return 0; } 

He produces.

 Daygood DaygoodGood 

In my both test cases, I get two different lines for comparison. Why then strcmp() creates 0?

+4
source share
6 answers

A quick way to get an answer without tracking all calls: both arguments to strcat returned from strcpy with the first arg str3 , and since strcpy returns its first arg argument, this means the final call to strcmp(str3, str3) , which of course will be 0 , regardless of what unusual manipulations were made over him.

In response to an updated question, try this and see if you are enlightened:

 #include <stdio.h> #include <string.h> int main(void) { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; char *first, *second; printf("str3 = %p => %s\n", (void *)str3, str3); first = strcat(str3, strcpy(str2, str1)); printf("first strcat returned %p => %s\n", (void *)first, first); printf("and now str3 = %p => %s\n", (void *)str3, str3); second = strcat(str3, "good"); printf("second strcat returned %p => %s\n", (void *)second, second); printf("and now first = %p => %s\n", (void *)first, first); printf("and now str3 = %p => %s\n", (void *)str3, str3); printf("Is it any surprise that strcmp(first,second) = %d?\n", strcmp(first,second)); return 0; } 
+3
source

Depending on which compiler chooses to calculate strcmp arguments, strcat always returns its first argument.

Therefore, in essence, this is what happens:

 ... // execute strcat(str3, strcpy(str2, str1)) and strcat(str3, "good") l = strcmp(str3, str3); 
+3
source

It returns 0 because both parameters:

 strcat(str3, strcpy(str2, str1)) 

and

 strcat(str3, "good") 

actually returns the same thing: the memory address assigned to str3. So strcmp returns 0 because it compares the variable str3 with itself.

+2
source

The fact that strcat always returns the first argument passed to it always makes your expression true. here is the explanation:

 strcmp(strcat(str3, strcpy(str2, str1)), strcat(str3, "good")); // ^^^^ ^^^^ // become strcmp( str3, str3 ); 

So strcmp return 0 , comparing the variable with itself.

You should know that this expression is not very good, because it makes the code less understandable and can lead to undefined behavior faster than you could imagine ...

+1
source

strcmp receives two line pointers as arguments:

  l = strcmp(strcat(str3, strcpy(str2, str1)), strcat(str3, "good")); 

1st step:

 l = strcmp(str3, strcat(str3, "good")); 

Here str3 points to the line DayGoodgoodGood.

Second step:

  l = strcmp(str3,str3 ); 

Now str3 points to the line DayGoodgoodGoodgood.

This will return 0 no matter what str3 points to. Since the addresses of the same strcmp should not be compared even for optimization; just returns 0.

+1
source

strcat returns its first argument why!

0
source

Source: https://habr.com/ru/post/1493855/


All Articles