|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我现在在学习结构体,刚刚写程序的时候碰到一个奇怪的地方
scanf("%d %f",&p1->num,&p1->score);
printf("%d %f\n",p1->num,p1->score);
这样输出的结果不是预期的!
scanf("%d",&p1->num);
scanf("%f",&p1->score);
printf("%d %f\n",p1->num,p1->score);
而这样输出的结果和预期的吻合!
请问这两种scanf的输入方法区别在哪?
这是整个程序的代码
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student) //student结构的大小
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
int n;
struct student *head,*p1,*p2,*p;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
n=0;
scanf("%ld,%f",&p1->num,&p1->score);
for(;p1->num!=0;)
{
n=n+1;
if(n==1)
{
head=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
else
{
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
}
p2->next=NULL;
p=head;
if(head)
{
do
{
printf("%ld %f\n",p->num,p->score);
p=p->next;
}while(p);
}
}
|
|