+ 1
why does this code show the following output?
code: #include <stdio.h> #include <string.h> int main() { char s1[ ] = "The grey fox"; char s2[ ] = " jumped."; strcat(s1, s2); printf("%s\n", s1); printf("Length of s1 is %d\n", strlen(s1)); strcpy(s1, s2); printf("s1 is now %s \n", s1); return 0; } output: The grey fox jumped. Length of s1 is 20 s1 is now jumped. *** stack smashing detected ***: <unknown> terminated Aborted (core dumped)
2 Respuestas
+ 4
char s1[ ] = "The grey fox";
s1 is char array of 13 bytes
when you call
strcat(s1, s2);
you do buffer overflow because your need 21 byte to store concatenated string but s1 can contain only 13.
declare s1 as
char s1[50] = "The grey fox";
to reserve space for the enlarged string.
+ 2
test