|
发表于 2020-5-8 21:00:01
|
显示全部楼层
void fun(char *in,char *out),
如果想在fun中为out分配空间的话是很危险的,因为你不知道什么时候释放他。
要看你的fun想实现什么功能。
如果只是想最加字符之类的可以这样做。
void fun(char* in, char* out)
{
int i = strlen(in);
int i1 = strlen(out);
char* szBuff = new char[i1+1];
strcpy( szBuff, out );
if(out){ delete out; out=NULL;}
out = new char[i+i1+1];
sprintf( out, "%s%s", szBuff, in );
delete []szBuff;
}
void main()
{
char* out = new char[6];
strcpy( out, "Hello" );
fun( " LZ", out );
printf( "%s\n", out );
delete []out;
} |
|