这个程序的SCANF语句为什么会没有效果,好像没有执行到,高手请指点一下!!

来源:百度知道 编辑:UC知道 时间:2024/05/04 08:34:41
/* Note:Your choice is C IDE */
#include "stdio.h"
typedef struct note
{int num;
struct note *next;
}stu;
main()
{stu *head=(stu *)malloc(sizeof(stu));
int n;
void setup(stu *head,int n);
void read(stu *head);
printf("please enter amount:");
scanf("%d\n",&n);
setup(head,n);
read(head);

}
void setup(stu *head,int n)
{int i,k=1;
stu *p=head,*s;
for(i=0;i<n;i++)
{s=(stu *)malloc(sizeof(stu));
s->num=k++;
p->next=s;
p=s;}
p->next=NULL;
}
void read(stu *head)
{stu *p=head;
printf("head");
while(p->next)
{p=p->next;
printf("->%d",p->num);}
printf("\n");
}

首先需要说明的是,你并没有引入malloc的头文件,须在开头加上:#include <malloc.h>(如果不行,试加#include <stdlib.h>)

问题在此:在scanf格式符后加\n是多此一举,把它删掉即可,否则,读入不符合格式符规则,就会出现输入错误。