#include
#include
//char temp[40];
char ha(char *in)
{
int i;
char *temp,*s;
temp=(char *)malloc(sizeof(char)(40));
for(i=0;i<40;++i)
temp[i]=in[i];
temp[i]='\0';
s=(char )malloc(sizeof(char)(4));
return temp;
}
int main()
{
char p[]="aaaaabbbbbcccccdddddaaaaabbbbbcccccddddd";
// scanf("%s",p);
printf("%s\n",ha(p));
return 0;
}
代码如上,最后的'\0'多占了一个字节的空间,然后在分配内存就会出错,问题在于只有字符串长度为40的时候会有问题,不知为何?