关于C语言realloc函数的问题
准备用一段代码测试realloc函数的功能,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char buf[100];
char** ps=(char**)calloc(2,sizeof(char*));
char** pt=NULL;
int i=0;
size_t len=0;
while(true)
{
fgets(buf,100,stdin);
if(buf[0]=='\n')
break;
len=strlen(buf)+1;
ps[i]=(char*)malloc(len);
strcpy(ps[i++],buf);
}
printf("%s",ps[1]);
pt=(char**)realloc(ps,3);
ps=pt;
printf("%s",ps[1]);
for(int k=0;k<3;++k)
free(ps[k]);
free(ps);
return 0;
}
程序没有报错,但运行会停止工作。调试后发现经过realloc函数后,ps[1]产生expression can not be evaluated错误,有大神知道原因吗?