鱼C论坛

 找回密码
 立即注册
查看: 3131|回复: 5

这段代码为什么执行到最后不输出,就退出了?

[复制链接]
发表于 2011-8-10 18:17:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define NULL ((void *)0)
  4. #define LEN sizeof(struct student)
  5. struct student
  6. {
  7. long num;
  8. float score;
  9. struct student *next;
  10. };
  11. int n;
  12. struct student *creat(void)
  13. {
  14. struct student *head;;
  15. struct student *p1,*p2;
  16. n=0;
  17. p1=p2=(struct student*)malloc(LEN);
  18. scanf("%ld,%f",&p1->num,&p1->score);
  19. head=NULL;
  20. while(p1->num!=0)
  21. {
  22. n=n+1;
  23. if(n==1)
  24. head=p1;
  25. else
  26. p2->next=p1;
  27. p1=p2;
  28. p1=(struct student*)malloc(LEN);
  29. scanf("%ld,%f",&p1->num,&p1->score);
  30. }
  31. p2->next=NULL;
  32. return(head);
  33. }
  34. void print(struct student *head)
  35. {
  36. struct student *p;
  37. printf("\nNow,These %d records are:\n",n);
  38. p=head;
  39. if(head!=NULL)
  40. do
  41. {
  42. printf("%ld %5.1f\n",p->num,p->score);
  43. p=p->next;
  44. }
  45. while(p!=NULL);
  46. }
  47. main()
  48. {
  49. creat();
  50. void print(struct student *head);
  51. system("pause");

  52. }
复制代码
是主函数调用的问题吗?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-10 19:21:33 | 显示全部楼层
第三行#define NULL ??????
NULL可以定义吗
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-10 20:02:55 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-20 20:15:09 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

//#define NULL ((void *)0)

#define LEN sizeof(struct student)

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(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

head=NULL;

while(p1->num!=0)

{

n++;//n++的效率会高一点儿吧if(n==1)

head=p1;

else

p2->next=p1;

p1=p2;

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);

}
}

main()

{
        struct student *head;
        void print(struct student *head);//这里是函数声明不是调用        head=creat();

        print(head);
        system("pause");


}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-8-20 20:16:40 | 显示全部楼层
不好意思,那个代码错乱了,但是我是想说明问题的所以特地用了红颜色的,你把那个head=creat()自己移下来吧,好看点儿!
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-8-20 20:18:33 | 显示全部楼层
响当当 发表于 2011-8-20 20:15
#include
#include
#include

灰常感谢,这个问题已经解决了
你指出的问题
我已经改正了
尽管这样
还是很
感谢
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 12:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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