http://bbs.freekaoyan.com/viewthread.php?tid=331864
哪位高手帮忙看看,这个C程序为什么不能在Tubor C上运行?
附件
2008-11-3 22:36
下载次数: 5
---------------------------------
这么短的程序,下次直接贴就行了。
弄个word,给你调程序还得费考元,你觉得有几个人愿意下啊?
还费论坛资源。
---------------------------------
能告诉我你的程序想干吗么?
你把struct student *creat(void)中void删了就能运行了。
不过真的不知道你这个程序是干涉么的。
运行过程一点提示都没有。输入什么都不知道。我也没时间猜了,你照着改了自己调吧。
---------------------------------
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;float score;struct student *next;};
int n;
struct student *creat()?/*这个函数用于创建链表,返回值为链表的头指针*/ {struct student *head;
struct student *p1,*p2;n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n 1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);/*输入学生的学号和分数*/ }
p2->next=NULL;
return(head);
}
void print(struct student *head)/*这个函数用于输出这个链表*/ { struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
void main()
{ struct student *head;
head=creat();/*创建链表*/ print(head);/*输出链表*/ printf("\n");
}
这其实是C程序设计(第三版)(谭浩强)上的几个小程序,被我整合成一个程序,编译没错,但运行时输入数据后出现错误,真的很郁闷,望高手指点。
---------------------------------
这个程序大部分是对的啊,吧main前的void去掉,然后去掉#define NULL 0
即可,我在DEV-C 下运行,结构正确噢
---------------------------------
struct student *creat()?/*这个函数用于创建链表,返回值为链表的头指针*/ {struct student *head;
就这一句,把creat()后的?去掉就全好了。
你那个问号哪儿来的?
---------------------------------
对着呢,我在VC 下只删除了 create()后的 ? 就可以运行了 没错啊。
根据你的程序,我提一下建议:
在从前往后建链表时用 do.....while()
输出时 用while()
更为合适
呵呵呵
---------------------------------
没结果 可能隐含指针错的原因
指针悬空 p->next不空才可取p->next的成员
建链表时 不要指错了 或忘了 指向 等
