An issue about 2D array in C

xlttap 2010-11-03 02:14:18
char **sss;
sss = (char**)calloc(5,sizeof(char*));
for (int i = 0; i < 5; i++) {
sss[i] = (char*)calloc(256,sizeof(char));
strcpy(sss[i],"aaa");
}

sss = (char**)realloc(sss,3*sizeof(char*));
for (int i = 0; i < 3; i++) {
sss[i] = (char*)calloc(256,sizeof(char));
strcpy(sss[i],"aaa");
}


for (int i = 0; i < 8; i++) {
printf("sss = %s\n",sss[i]);
free(sss[i]);
}
free(sss);

Assue: It seems as if ''realloc" not works from the result printed.
...全文
47 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlttap 2010-11-03
  • 打赏
  • 举报
回复
I have been solved. haha

sss = (char**)realloc(sss,8*sizeof(char*));
for (int i = 0; i < 3; i++) {
sss[5+i] = (char*)calloc(256,sizeof(char));
strcpy(sss[5+i],"aaa");
}

It works OK! excellent. If the size realloced is less than the old size, the array
doesn't change any way.
xy_zhang 2010-11-03
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/en-us/library/xbebcx7d%28VS.71%29.aspx

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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