我有以下程序无法合并两个字符串,因为string1没有足够的空间来容纳合并的字符串。当string1没有足够的空间并且没有使用额外的数组来保存合并的字符串如何返回合并的字符串?如何合并两个字符串
#include<stdio.h>
#include<string.h>
int main()
{
void strcat2(char *str1,char *str2);
strcat2("john","kris");
getchar();
}
void strcat2(char *str1,char *str2)
{
for (; *str1++;);
for (;*str1++ =*str2++;);
}
这么多**未定义的行为**,这么短的时间。 – WhozCraig
这可能是有用的http://stackoverflow.com/questions/308695/c-string-concatenation – 2013-08-22 18:01:52
使用malloc分配合适的空间来保存结果。 –