- 注册时间
- 2007-6-1
- 最后登录
- 2019-7-1
②入门
- 积分
- 13
|
程序如下- #include<stdio.h>
- #define NULL 0
- #define LEN sizeof(struct student)
- struct student
- {
- long num;
- float pay;
- 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->pay);
- 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->pay);
- }
- p2->next=NULL;
- return(head);
- }
- void list(struct student *head)
- {
- struct student *p;
- printf("\n now .the %d records are:\n",n);
- p=head;
- if(head!=NULL)
- do{
- printf("%ld %f\n",p->num,p->pay);
- p=p->next;
-
- }while(p!=NULL);
- }
- main()
- {
- struct student *head;
- printf("input records:\n");
- head=creat();
- list(head);
- }
复制代码
为什么这程序在C++里可运行,但在C里不能呢?在C里只能执行一次,然后就提示错为
“scanf: floating point formats not linked Abnormal program termination”
怎么会这样呢?
还有就是开始的那几句
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->pay);
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->pay);
}
p2->next=NULL;
return(head);
应该如何理解呢? |
|