Here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char f[] = "First";
char s[] = "Second";
char *tmp = malloc(strlen(f) + strlen(s) + 2);
strcpy(tmp, f);
strcpy(tmp, s);
printf("%s", tmp);
free(tmp);
return 0;
}
I’m trying to concatenate f and s. The problem is that tmp contains only “Second” as a array.
What I miss here
If you insist on using
strcpy, your code should be slightly modified:You should consider using
strncpyinstead ofstrcpyfor safety reasons. Also,strcatis a more conventional function for concatenating C string.EDIT Here is an example of using
strncpyinstead ofstrcpy