关于C语言realloc函数的问题

criedjoker 2015-07-10 03:27:05
准备用一段代码测试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错误,有大神知道原因吗?
...全文
329 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧