鱼C论坛

 找回密码
 立即注册
查看: 5798|回复: 9

为什么输入0不结束程序?

[复制链接]
发表于 2013-5-7 14:07:29 | 显示全部楼层 |阅读模式

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

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

x
初学链表,刚刚才弄懂书上的例题,合起书自己写了下代码,要求是:创建一个链表,该链表可以存放任意长度的名字,输入0表示结束输入.
但是我输入0发现跟本结束不了,求解.自己调试发现p2->name输入之后还是空的,所以while判断不起作用,是什么原因我找不出来,求解!

代码:

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define NULL 0
  4. #define LEN sizeof(struct student)

  5. struct student
  6. {
  7.         char *name;
  8.         struct student *student_next;
  9. };

  10. struct student *creat(struct student *h);
  11. void print_student(struct student *h);

  12. void main()
  13. {
  14.         struct student *head;
  15.         head=NULL;
  16.         printf("请输入学生的名字,以输入零结束输入:\n");
  17.         head=creat(head);
  18.         print_student(head);
  19. }

  20. struct student *creat(struct student *h)
  21. {
  22.         struct student *p1,*p2;
  23.         p1=p2=(struct student *)malloc(LEN);
  24.         if(p2!=NULL)
  25.         {
  26.                 scanf("%c",&p2->name);
  27.                 p2->student_next=NULL;
  28.         }

  29.         while(p2->name!='0')
  30.         {
  31.                 if(h==NULL)
  32.                 {
  33.                         h=p2;
  34.                 }
  35.                 else
  36.                 {
  37.                         p1->student_next=p2;
  38.                 }
  39.                 p1=p2;
  40.                 p2=(struct student *)malloc(LEN);
  41.                 if(p2!=NULL)
  42.                 {
  43.                         scanf("%c",&p2->name);
  44.                         p2->student_next=NULL;
  45.                 }
  46.         }

  47.         return h;
  48. }

  49. void print_student(struct student *h)
  50. {
  51.         struct student *temp;
  52.         temp=h;
  53.         while(temp)
  54.         {
  55.                 printf("%c  ",temp->name);
  56.                 temp=temp->student_next;
  57.         }
  58. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-7 14:10:47 | 显示全部楼层
强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-7 14:39:55 | 显示全部楼层
建议楼主:别把 '0' 和 'NULL' 混一起。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-7 15:04:42 From FishC Mobile | 显示全部楼层
GeekDream 发表于 2013-5-7 14:39
建议楼主:别把 '0' 和 'NULL' 混一起。

都应该是一样的吧……
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-7 16:08:11 | 显示全部楼层
  1.                 scanf("%c",&p2->name);
复制代码
你上面定义了char *name。。。。。。。。。这里的输入有问题吧
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-7 16:18:36 From FishC Mobile | 显示全部楼层
喜欢散步 发表于 2013-5-7 16:08
你上面定义了char *name。。。。。。。。。这里的输入有问题吧

后来我改成%s,不过输出的是个如果输入的是汉字的话就会输出乱码…输入字母就不会,不知道什么情况……………
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-7 18:11:27 | 显示全部楼层
  1. scanf("%c",&p2->name);
复制代码
改成:
  1. scanf("%s",p2->name);
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-7 18:44:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-5-7 18:45:56 | 显示全部楼层

我想应该是while那么括号哪里出错了,不知知道改成什么是真确的....
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-5-7 18:58:24 | 显示全部楼层
强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-29 03:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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