|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么第二个scanf还没输入程序就结束了?
[图片]
#include<stdio.h>
#include<stdlib.h>
#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);//使用指针之前必须要让ta有指向,即开辟一个空间
scanf("%1d,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;//表示第一个结点
if(n==1)
head=p1;//把头指向第一个结点
else p2->next=p1;
p2=p1;
p2=p1=(struct Student *)malloc(LEN);
scanf("%1d,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
int main()
{
struct Student *pt;
pt=creat();
printf("\nnum:%1d\nscore:%5.1f\n",pt->num,pt->score);
return 0;
}
你的程序错误是由于敲代码的时候粗心导致的
scanf中第一个%后面你洗的是1d,应该是ld,你把字母l写成数字1了
%1d意思是读取一位整数,%ld才是读取一个长整型
当然楼上几位说的也很好,scanf中尽量不要有其他字符,两个格式化符号完全可以直接挨着,反正输入的时候是需要用空白符来表示一个数据的结束的
scanf("%ld%f",&p1->num,&p1->score);
|
|