First off, you allocated a single char and then try to copy an array of chars to it! Memory corruption!
And this does nothing since pos isn't incremented:
for(i = 0; i < size; i++)
result[pos] = str2[i];
So if you really want to allocate new memory, declare it like:
char * result = new char[GetLength(str1) + GetLength(str2) + 1];
Next, keep in mind that you will have to deallocate the memory on th calling side every time you use this function:
char *s = StrComb("The Cat ", "in the Hat", 20);
//...use it...
delete[] s;
Also, if returning new memory, why use the third parameter at all?
The standard strncat copies the second string to the first, exceeding no more than string one's total size, then returns a pointer to the first element of string one. Keeping that in mind we have:
Code:
char * StrComb(char * str1, char * str2, int size)
{
int i = 0, k = 0, length = GetStringLengh( str2);
while( str[i] != 0 ) i++; //...reach null terminator.
while( i < size && k < length)
{
str1[i++] = str2[k++];
}
str1[i] = 0; //...null term.
return str1; //...return a pointer to str1.
}