The best way is to do use malloc to allocate the memory to copy it into, then use memcopy - as Richard says, trying to change a constant string can crash your app.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
printf("Hello World\n");
char *A = "Hello";
char *B = " World";
int lenA = strlen(A);
int lenB = strlen(B);
char *A3 = (char*) malloc(lenA + lenB + 1);
memcpy(A3, A, lenA);
memcpy(A3 + lenA, B, lenB);
A3[lenA + lenB] = '\0';
printf("\n%s\n",A3);
return 0;
}
malloc provides a way to allocate memory of a variable size - so it's easier to write a function that can be called repeatedly to do the copy to fresh memory:
char* AppendStrings(const char *A, const char*B)
{
int lenA = strlen(A);
int lenB = strlen(B);
char *C = (char*) malloc(lenA + lenB + 1);
memcpy(C, A, lenA);
memcpy(C + lenA, B, lenB);
C[lenA + lenB] = '\0';
return C;
}
Then you can call it with whatever you want:
char * C = AppendStrings(A, B);
printf("\n%s\n",C);
char * D = AppendStrings("Hello World!\n", "This is a test");
printf("\n%s\n",D);