scanf()函数深入研究,求解
#include <stdio.h>#include <math.h>
struct infor /////建立一个结构体
{
long num;
char classify[2];
char name[20];
char author[30];
float price;
struct infor *next;
};
void main()
{
float temp=sqrt(0);
int n=0;
char ch;
struct infor *head;
struct infor *p1, *p2; /////建立链表,没有错,已验证。
p1=p2=(struct infor*)malloc(sizeof(struct infor));
printf("\nPlease input num :");
scanf("%ld",&p1->num);
ch=getchar(); ////吃回车,你懂的。
printf("\nPlease input classify:");
scanf("%s",p1->classify); ////问题来了,如果这里我输入aa ,
ch=getchar();
printf("\nPlease input books'name :");
scanf("%s",p1->name); 这里再输入bb,你们猜会怎么样,系统显示,P1->classify='aabb';
ch=getchar(); 我就纳闷了,为什么我明明定义classify是2个字节的数组,它却能
printf("\nPlease input author's name:"); 无缘无故地多装2个字节?
scanf("%s",p1->author);
ch=getchar();
printf("\nPlease input price of the book:");
scanf("%f",&p1->price);
ch=getchar();
printf("--------------------------");
head=0;
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct infor *)malloc(sizeof(struct infor));
printf("--------------------------");
printf("\nPlease input num :");
scanf("%ld",&p1->num);
ch=getchar();
printf("\nPlease input classify:");
scanf("%s",p1->classify);
ch=getchar();
printf("\nPlease input books'name :");
scanf("%s",p1->name);
ch=getchar();
printf("\nPlease input author's name:");
scanf("%s",p1->author);
ch=getchar();
printf("\nPlease input price of the book:");
scanf("%f",&p1->price);
ch=getchar();
}
p2->next=NULL;
}