看不懂你的逻辑,并且本身这个写法就是不正确的,没有内存临界检查很有可能会造成缓冲区溢出。
字符串连接可以使用strcat,注意目标string必须有足够大的内存。
比如
char* dest = (char*)malloc(sizeof(char)*1024); // here we allocate 1024 byte to the string
strcpy ( dest, "hello " ); // so at here, dest was allocated for 1024 bytes, but only used 7 bytes: h,e,l,l,o,<space>,</0>
char* src = "world!" ; // src has 7 bytes: w,o,r,l,d,!,</0>
if ( strlen(dest)+strlen(src)+1 <= 1024 ) // check if 1024 byte is enough for the whole string, remember to add 1 for </0>
{
dest = strcat ( dest, src ); // concat the 2 strings
}
else
{
return ENOMEM ;
}