I would like to concatenate two string with memcpy. But next memcpy is not working. My expected output is "my name is khan".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *var1 = "my name";
char *var2= "is khan";
char *f_add[20];
memcpy(f_add,var1, strlen(var1)+1);
memcpy(f_add+8,var2, strlen(var2)+1);
printf("%s", f_add);
return 0;
}
mallocforf_addandchar *f_add[20];is not the right one to do concatenationmemcpyand notstrcpy&strcat?strcpy( f_add, var1 ); strcat( f_add, var2 );(and definef_addcorrectly). PS: You problem (one of all) is this+8- you need+7because 8 is beyond the zero, i.e. second string is ignored.