Here's how I would handle it, using existing string commands instead of memcpy. I'm assuming you want something like strcat that doesn't affect the source strings.
char* string_concat(char *dest, const char* string1, const char* string2)
{
strcpy(dest, string1);
strcat(dest, string2);
return dest;
}
To use it, you need to pass in a pointer to the buffer you want the result stored in. You can use malloc to make it the size you need. Free it when you're done.
char *str1 = "abc";
char *str2 = "def";
size_t len = strlen(str1) + strlen(str2);
char *newstr = malloc(len + 1);
string_concat(newstr, str1, str2);
printf("%s\n", newstr);
free(newstr);
There is simply no way to deal with arbitrary-length strings without allocating memory, so you'll be stuck with malloc/free unless you're using character arrays with fixed lengths. If you want to abstract the logic of deciding how big of a buffer to allocate you can do something like this:
size_t string_concat(char* dest, char* string1, char* string2)
{
if(!dest)
{
return strlen(string1) + strlen(string2) + 1;
}
strcpy(dest, string1);
strcat(dest, string2);
return 0;
}
Then you can ask it how much to allocate like this:
char* newstr = malloc(string_concat(0, str1, str2));
But you lose the syntactical convenience of it returning a pointer to dest.