Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcat() crashes if using same array as both parameters

char r[40];
strcpy(r,"abcdef");
strcat(r,r);

My program crashes at the third line?

Replacing strcat(r,r); by strcat(r,"abcdef"); works fine though.... why is that?

like image 724
user1420563 Avatar asked Nov 30 '25 20:11

user1420563


1 Answers

According to strcat(3):

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.

like image 67
Lee Duhem Avatar answered Dec 03 '25 12:12

Lee Duhem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!