让人郁闷的函数 --为什么只能做主函数却不 能做子函数呢?

languagec 2003-12-12 03:29:49
#include"stdio.h"
#include"stdlib.h"
struct elem
{
int qu;
char elem;
struct elem *next,*left,*right;
};
struct elem *creattree()
{
struct elem *head,*last,*this,*new;
char c;
int size;
head->next=last;
last->next=0;
size=sizeof(struct elem);
printf("please input char such as A ,then input num such as 8 :\n");
loop:
new=(struct elem *)malloc(size);
scanf("%c %d",&new->elem,&new->qu);
c=getchar();
last->qu=new->qu;
for(this=head;this->next!=0;this=this->next)
{
if(new->qu<=this->next->qu)
{
new->next=this->next;
this->next=new;
break;
}
}
switch(c)
{
case '\n':goto loop;
defult :last=0;break;
}
return head;
}
main()
{
struct elem *head,*this;
this=head->next;
while(this->next!=0)
{
printf("%d %c\t",this->qu,this->elem);
this=this->next;
}
}
...全文
42 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
languagec 2003-12-12
  • 打赏
  • 举报
回复
问题解决 ,但同志们怎么不早说……
expiry 2003-12-12
  • 打赏
  • 举报
回复
同意楼上的
fierygnu 2003-12-12
  • 打赏
  • 举报
回复
struct elem *head,*last,*this,*new;
char c;
int size;
head->next=last; //head和last都没有分配空间
last->next=0;
fansings 2003-12-12
  • 打赏
  • 举报
回复
struct elem
{
int qu;
char elem;
struct elem *next,*left,*right;
};

没遇到过成员跟结构类型名一样的?

ryyy 2003-12-12
  • 打赏
  • 举报
回复
那是因为你这个用作子程序的话,啥都不返回。
加上 head=(struct elem *)malloc(sizeof(char)*sizeof(struct elem));
last=(struct elem *)malloc(sizeof(char)*sizeof(struct elem));
子程序return前free(last);

main结束前free那个head,
languagec 2003-12-12
  • 打赏
  • 举报
回复
调用子程序时运行不正常。不能输出 输入的数据。
把子程序改为主程序,运行正常,能正常输出数据。
我说的就是这个。
languagec 2003-12-12
  • 打赏
  • 举报
回复
复制时掉了一句,加上!
main()
{
struct elem *head,*this;
head=creattree();
this=head->next;
while(this->next!=0)
{
printf("%d %c\t",this->qu,this->elem);
this=this->next;
}
}
ryyy 2003-12-12
  • 打赏
  • 举报
回复
creattree这个函数用都没用啊
ryyy 2003-12-12
  • 打赏
  • 举报
回复
说实话,你在说什么?
languagec 2003-12-12
  • 打赏
  • 举报
回复
以上是给输入数据按大小排序的程序,子函数返回链表头指针,可是为什么运行出错呢?
当输入数据后,提示“非法操作”。

70,024

社区成员

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

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