幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2367|回复: 1

[通用编程] 运行的问题

[复制链接]

1

主题

1

帖子

13

积分

②入门

积分
13
发表于 2007-6-2 04:21:43 | 显示全部楼层 |阅读模式
程序如下
  1. #include<stdio.h>
  2. #define NULL 0
  3. #define LEN sizeof(struct student)
  4. struct student
  5. {
  6.     long num;
  7.     float pay;
  8.     struct student *next;
  9. };
  10. int n;
  11. struct student *creat()
  12. {
  13.     struct student *head;
  14.     struct student *p1,*p2;
  15.     n=0;
  16.     p1=p2=(struct student *)malloc(LEN);
  17.     scanf("%ld,%f",&p1->num,&p1->pay);
  18.     head=NULL;
  19.     while(p1->num!=0)
  20.     {
  21.         n=n+1;
  22.         if(n==1) head=p1;
  23.         else
  24.             p2->next=p1;
  25.         p2=p1;
  26.         p1=(struct student *)malloc(LEN);
  27.         scanf("%ld,%f",&p1->num,&p1->pay);
  28.     }
  29.     p2->next=NULL;
  30.     return(head);
  31. }
  32. void list(struct student *head)
  33. {
  34.     struct student *p;
  35.     printf("\n now .the %d records are:\n",n);
  36.     p=head;
  37.     if(head!=NULL)
  38.         do{
  39.             printf("%ld %f\n",p->num,p->pay);
  40.             p=p->next;
  41.         
  42.         }while(p!=NULL);
  43. }
  44. main()
  45. {
  46.     struct student *head;
  47.     printf("input records:\n");
  48.     head=creat();
  49.     list(head);
  50. }
复制代码

为什么这程序在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);
应该如何理解呢?
回复

使用道具 举报

20

主题

197

帖子

2641

积分

⑥精研

积分
2641
QQ
发表于 2007-6-2 06:04:40 | 显示全部楼层
第一个问题尚未测试估计是由于你在while中时由于重复分配内存的原因.....
第二个问题该函数实现了链表结构....head代表链表的头....
while循环的工作是添加链表数据....p2最后的指向表示了链表结尾.....
签名要少于60,SO,i haven't upload my pic
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-4-28 20:47 , Processed in 0.023668 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表