求助。一段有关结构和联合方面代码出现 segmentation fault。
本人初学C,编了一段练习结构和联合知识方面的代码。但debug时出现 c programme received signal sigsegv segmentation fault不明其原因,请有时间的前辈指点。
代码如下(本人初学,代码风格很挫请见谅)
目的:输入每个学生的学号,姓名,英语数学物理设计四门成绩。然后输出每个学生的各项信息。
#include <stdio.h>
#include "stdlib.h"
struct xinxi{
int xuehao;
char *xingming;
float yingyu;
float shuxue;
float wuli;
float sheji;
struct xinxi *p;
};
void create_list (struct xinxi **headp);
int main(void)
{
struct xinxi *head=NULL,*pp;
create_list(&head);
pp=head;
while(pp){
printf ("xuehao:%d\tname:%s\n",pp->xuehao,pp->xingming);
printf ("yingyu:%f\tshuxue:%f\twuli:%f\tsheji:%f\n",pp->yingyu,pp->shuxue,pp->wuli,pp->sheji); /* 输出数据域的值 */
pp=pp->p;
}
printf("\n");
return 0;
}
void create_list(struct xinxi **headp)
{
struct xinxi * loc_head=NULL,*tail;
int xuanze;
loc_head=(struct xinxi *)malloc(sizeof(struct xinxi));
printf ("xuehao,name:\t");
scanf ("%d,%s",&loc_head->xuehao,loc_head->xingming);
getchar();
printf ("yingyu,shuxue,wuli,sheji: ");
scanf ("%f,%f,%f,%f",&loc_head->yingyu,&loc_head->shuxue,&loc_head->wuli,&loc_head->sheji);
getchar();
loc_head->p=NULL;
printf ("if you want to enter next one input '1'\n " );
scanf ("%d",&xuanze);
while(1==xuanze)
{
tail=(struct xinxi *)malloc(sizeof(struct xinxi));
tail->p=loc_head;
printf ("xuehao,name:\t");
scanf ("%d,%s",&tail->xuehao,tail->xingming);getchar();
printf ("yingyu,shuxue,wuli,sheji: ");
scanf ("%f,%f,%f,%f",&tail->yingyu,&tail->shuxue,&tail->wuli,&tail->sheji);getchar();
loc_head=tail;
printf ("if you want to enter next one input 1\n " );
scanf ("%d",&xuanze);
}
*headp=loc_head;
}