结构体中字符串输出出错
#include "stdio.h"#include "malloc.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
struct student
{
long int num;
char name[20];
char class_type[5];/*j is junior college,g is graduate,u is undergraduate */
int tuition;
int back_charge;
struct student *next;
};
struct student *creat(void);
struct student *print(struct student *head);
int n;
int j_tuition;
int a;
main()
{
struct student *head=NULL;
head=creat();
print(head);
getch();
}
struct student *creat(void)
{
struct student *p1,*p2,*head;
clrscr();
n=0;
head=NULL;
p1=p2=NULL;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("input the junior college student tuition:\n");
scanf("%d", &j_tuition);
printf("input number junior college:0 undergraduate:1 or graduate:2:\n");
scanf("%d", &a);
if(a==1)
j_tuition *=0.8;
else if(a==2)
j_tuition *=0.56;
printf("input the student num:\n");
scanf("%d", &p1->num);
while(p1->num != 0)
{
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
printf("\ninput the student name:\n");
scanf("%s", p1->name);
printf("\ninput the student class type j,u or g:\n");
scanf("%s", p1->class_type);
printf("\ninput the student tuition:\n");
scanf("%d", &p1->tuition);
p1->back_charge=j_tuition - p1->tuition;
n++;
p1=(struct student *)malloc(sizeof(struct student));
printf("\ninput the student num:\n");
scanf("%d", &p1->num);
}
p2->next=NULL;
return(head);
}
struct student *print(struct student *head)
{
struct student *p;
if(head==NULL)
printf("\nNo student information!\n");
else
{
printf("the number of studnet is %d\n",n);
printf("................................................\n");
printf("|num\t|name\t|class_type\t|tuition\t|back_chrge|\n");
printf("................................................\n");
p=head;
do
{
printf("|%d\t|%s\t|%s\t|",p->num,p->name,p->class_type);
printf("%d\t|%d\t|\n", p->tuition, p->back_charge);
printf("............................................\n");
p=p->next;
}while(p!=NULL);
}
}
输出出错,第一个字符串name输出为NULL