建链表一个奇怪的现象 急!

shendu21 2010-11-28 10:56:47
#include<stdio.h>
#include<malloc.h>
struct student
{
long num ;
float score;
struct student * next;
};

struct student *creat(void)
{
struct student *head ;
struct student *p1,*p2;
int n=0;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
p1->next=NULL;
p2=p1;
head=p1;
while(p1->num!=0)
{
n=n+1;
printf("%d\n",n);
p1=(struct student * )malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
p1->next=NULL;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return(head);
}
void main()
{
creat();
}
问题:执行一次scanf("%ld,%f",&p1->num,&p1->score);但执行了2次printf("%d\n",n);也就是说当你输入1 23
会打印 1 2(应该只打印1啊)
thanks!
...全文
37 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
edwardliqi 2010-11-29
  • 打赏
  • 举报
回复
DEBUG一下看看呗。。
ddyddl 2010-11-29
  • 打赏
  • 举报
回复
在这发帖都不给分:(

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧