大家帮忙看看这个程序哪里出错了?
大家帮忙看看这个程序哪里出错了?编译,链接,运行没有问题,可是输入数据的时候就出现内存不能读的问题!请大家帮忙看看!!题目是输入5个学生的身高体重和姓名,并存放在结构体数组中,从中查找出身高和体重最高的那个学生,并且输出他们的姓名,身高和体重!
程序代码:
#include <stdio.h> #include <stdlib.h> struct student { char name[8]; float high; long int weight; struct student *next; }; int n,i; struct student *creat() { i=1; struct student *p,*head,*stu; head=NULL; stu=p=(struct student *)malloc(sizeof(struct student)); printf("请输入学生 %d 的姓名身高体重:",i); scanf("%s,%f,%ld",&stu[i].name,&stu[i].high,&stu[i].weight); fflush(stdin); while(stu[i].high!=0) { n++; if(n==1) { head=stu; head->next=NULL; } else p->next=stu; p=stu; stu=(struct student *)malloc(sizeof(struct student)); printf("请输入学生 %d 的姓名身高体重:",++i); scanf("%s,%f,%ld",&stu[i].name,&stu[i].high,&stu[i].weight); fflush(stdin); } p->next=NULL; return(head); } int search(struct student *head) { struct student *stu; float t; int j,l; stu=head; t=stu[0].high; for(j=1;j<=i;j++) { if(t<stu[j].high) { t=stu[j].high; l=j; } } return l; } int find(struct student *head) { struct student *stu; long int t,j,k; stu=head; t=stu[0].weight; for(j=1;i<=i;j++) { if(t<stu[j].weight) { t=stu[j].weight; k=j; } } return k; } void main(void) { struct student *head,*stu; head=creat(); stu=head; int a,b; a=search(head); printf("%-5s%-5f%-5ld",stu[a].name,stu[a].high,stu[a].weight); b=find(head); printf("%-5s%-5f%-5ld",stu[b].name,stu[b].high,stu[b].weight); return; }