求助动态数组问题啊?

lqxqust 2009-04-29 11:20:21
int current_element=0;
int total_element=5;
char *dynamic;

void bufmalloc()
{
dynamic=(char*)malloc(total_element);
}

void add_element(char c)
{

if(current_element==total_element-1)
{
total_element*=2;
if((char*)realloc(dynamic, total_element)==NULL)
{
perror("Couldn't expand the table\n");
}
else
{
dynamic=(char*)realloc(dynamic, total_element);
}
}
current_element++;
dynamic[current_element]=c;
}
int main(void)
{
char c;
int i;
bufmalloc();
for(i=0;i<3;i++)
{
scanf("Input char:%c\n",&c);
add_element(c);
fflush(stdin);

}
printf("current_element is %d\n",current_element);
printf("dynamic[] is %d\n",dynamic[0]);
}
为什么dynamic数组输不出数据呢?
...全文
80 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqxqust 2009-04-29
  • 打赏
  • 举报
回复
刚才有些糊涂了,现在搞定了,非常感谢大家的指点啊.....
liliangbao 2009-04-29
  • 打赏
  • 举报
回复
注释:
int current_element=0; 
int total_element=5;
char *dynamic = NULL;

void bufmalloc()
{
dynamic=(char*)malloc(total_element);
if(dynamic == NULL)//判断一下
{
exit(1);
}
}

void add_element(char c)
{

if(current_element == total_element-1)
{
total_element*=2;
if((dynamic =(char*)realloc(dynamic, total_element))== NULL) //应该这样,原来那样内存泄漏
{
perror("Couldn't expand the table\n");
exit(1);
}
}
//current_element++;
dynamic[current_element++]=c; //将上一句放在赋值后面,否则0位置没有使用
}


int main()
{
char c;
int i;
bufmalloc();
for(i=0;i <3;i++)
{
scanf("%c",&c);//应该这样,scanf中别加和输入无关的字符
add_element(c);
fflush(stdin);
}
printf("current_element is %d\n",current_element);
printf("dynamic[] is %s\n", dynamic); //输出的是字符串

free(dynamic); //释放内存
return 0;
}
lqxqust 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 arong1234 的回复:]
更正一下注释

C/C++ code
if((char*)realloc(dynamic, total_element)==NULL)
{
perror("Couldn't expand the table\n");
}
else
{
dynamic=(char*)realloc(dynamic, total_element); //既然if的部分他已经扩展了内存,你再realloc干吗?
}
[/Quote]
呵呵 这个地方错了 我再试试上面说的那些 谢谢
lingyin55 2009-04-29
  • 打赏
  • 举报
回复
最后printf("dynamic[] is %d\n",dynamic[0]); 这个
只是输出第一个数组元素,如果你要输出整个输出,就得改成循环输出
lingyin55 2009-04-29
  • 打赏
  • 举报
回复
稍微改下,输出


Input char:a

Input char:b

Input char:c

current_element is 3
dynamic[] is a
请按任意键继续. . .




#include <stdio.h>
#include <stdlib.h>

int current_element=-1;
int total_element=5;
char *dynamic;

void bufmalloc()
{
dynamic=(char*)malloc(total_element);
}

void add_element(char c)
{

if(current_element==total_element-1)
{
total_element*=2;
if((char*)realloc(dynamic, total_element)==NULL)
{
perror("Couldn't expand the table\n");
exit(0);//////出错就直接退出了
}
//else /////////////////这里没必要重新分配,前面if已经执行了
//{
// dynamic=(char*)realloc(dynamic, total_element);
//}
}
current_element++;
dynamic[current_element]=c;
}
int main(void)
{
char c;
int i;
bufmalloc();
for(i=0;i <3;i++)
{
printf( "Input char:" );
scanf("%c",&c);
add_element(c);
fflush(stdin);
printf( "\n" );

}
printf("current_element is %d\n",current_element+1); /////////////
printf("dynamic[] is %c\n",dynamic[0]); /////////////////用%c输出
return 0;
}
arong1234 2009-04-29
  • 打赏
  • 举报
回复
更正一下注释

if((char*)realloc(dynamic, total_element)==NULL)
{
perror("Couldn't expand the table\n");
}
else
{
dynamic=(char*)realloc(dynamic, total_element); //既然if的部分他已经扩展了内存,你再realloc干吗?
}

lingyin55 2009-04-29
  • 打赏
  • 举报
回复
改为scanf("%c",&c);

[Quote=引用 2 楼 lingyin55 的回复:]
scanf("Input char:%c\n",&c);
改为scanf("Input char:%c",&c);
[/Quote]
arong1234 2009-04-29
  • 打赏
  • 举报
回复
current_element++;
dynamic[current_element]=c;
这里错误,应该是
dynamic[current_element]=c;
current_element++;

因为数组是以0为起始下标的,如果你先++,第0个元素就不会被赋值,有的时候(尤其debug时),malloc会把分配的数组清0,这种情况下,如果先++,第0个元素就是'\0', 恰好是字符串结束符,因此不会输出

另外:


if((char*)realloc(dynamic, total_element)==NULL)
{
perror("Couldn't expand the table\n");
}
else
{
dynamic=(char*)realloc(dynamic, total_element); //既然if的部分他已经失败了,你再调用有什么意义?
}

lingyin55 2009-04-29
  • 打赏
  • 举报
回复
scanf("Input char:%c\n",&c);
改为scanf("Input char:%c",&c);
lingyin55 2009-04-29
  • 打赏
  • 举报
回复
虽然应该没什么影响,不过还是改下号
dynamic=(char*)malloc(total_element);
dynamic=(char*)malloc(total_element*sizeof(char));

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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