程序如下:作用是建立链表并输出,学生的信息包括号码,姓名,籍贯。
在输入如: input records:
100,xiao,guangzhou(回车)
101,ming,shanghai(回车)
0,0,0(回车)
输出结果:Now,these 2 records are:
100,xiao,guangzhou(后面有乱码)
101,ming,shanghai(后面有乱码)
问题:怎么去掉那些乱码?
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
char name[15];
char bthplc[15];
struct student * next;
};
int n;
struct student * creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%s,%s",&p1->num,p1->name,p1->bthplc);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%s,%s",&p1->num,p1->name,p1->bthplc);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{struct student *p;
printf("\nNow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%ld,%s,%s\n",p->num,p->name,p->bthplc);
p=p->next;
}while(p!=NULL);
}
void main()
{struct student *head,stu;
printf("input records:\n");
head=creat();
print(head);
}
如果是数组越界那要怎么解决?