请问以下C程序哪错了?

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:57:17
#include"stdio.h"
#include"malloc.h"
#define NULL 0
struct student
{ long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
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(sizeof(struct student));
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.lf\n",p->num,p->score);

scanf("%ld,%f",&p1->num,&p1->score);
这里写错了,p1是指针那么&p1就是指针的地址,&p1跟p1不一样并不是一个struct student*类型的变量他应该是一个struct student** 类型。所以&p1->num就出错了。你应该写
scanf("%ld,%f",&(p1->num),&(p1->score));

我没有去测试过你的程序,但我看到这一处的错误。如果还有错的话你在发一个问题出来吧。10分就解决这么多了

在winTC下能通过,没有错误

->优先级高于&

wintc下载软件名称:WinTC1.9.1
软件大小:3454KB 软件类别:国产软件/编程工具
下载次数:98756 软件授权:免费版
软件语言:简体中文 运行环境:Win9x/Me/NT/2000/XP/2003
软件评级:5星

原始文件 p2s.newhua.com/down/wintc191.EXE

广州长宽 gzgwbn.onlinedown.net/down/wintc191.EXE

1楼的不是吧。。->的优先级比&高吧,你那括号加不加一样
p1->num 等同与(*p1).num
&(p1->num)和&p1->num效果一样,完全不是错在这里
在main函数的第一句前加上 float arg,*point=&arg;就OK了 我也是今天才发现这问题。。是TC2。0的浮点数BUG

#include"stdio.h"
#include"malloc.h"
中"stdio.h""malloc.h"要加括号

也许是版本问题,我试了DevC++ 没问题
你看看书上是不是vc++调试的
程序调试结果附录如下:
input records:
1010