char *str=NULL;
strsave(s,str,n+1);
printf("%s",str-n);
当我 gdb 调试此代码时,我发现 str 值为 0x0,它为空,而且我的代码没有捕获这个失败的内存分配,它不执行 str==NULL perror 代码...任何想法
void strsave(char *s,char *str,int n)
{
str=(char *)malloc(sizeof(char)* n);
if(str==NULL)
perror("failed to allocate memory");
while(*s)
{
*str++=*s++;
}
*str='\0';
}