Verbatim from man strcat
:
char *strcat(char *dest, const char *src);
DESCRIPTION
The strcat() function appends the src string to the dest string, overwriting the 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.
As the programmer you need to make sure that the pointer char * dest
references enough valid memory to hold the addtional char
s that will be copied from where char* src
points to.
To succesfully prefix str1
with str2
declare them as follows:
char str2[3 + 1] = "123"; // three for "123" plus 1 for the terminating 0
char str1[2 + 3 + 1] = "46"; // 2 for "46" plus 3 for "123" plus 1 for the terminating zero
to actually concatenate the two char
arrays do so:
strcat(str1, str2);