求解惑告诉我这个程序哪有问题,自定义函数Calate2好像发生了越界访问
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define N 30
#define M 6
struct student
{
long studentID;
char studentname[10];
int score[6];
int total;
float aver;
struct student *next;
};
struct student *Input_record(struct student *head,int n,int m); //1
void Calate1(struct student *head,int m,int n); //2
void Calate2(struct student *head,int n,int m); //3
void main()
{
int n,m;
struct student *head=NULL;
printf("how many subject?\n");
scanf("%d",&m);
printf("how many student?\n");
scanf("%d",&n);
head=Input_record(head,m,n);
Calate1(head,m,n);
Calate2(head,m,n);
}
struct student *Input_record(struct student *head,int m,int n)//1
{
int i,j;
struct student *p,*pr;
for(i=1;i<=n;i++)
{
p=NULL;
pr=head;
p=(struct student *)malloc(sizeof(struct student));
if(p==NULL)
{
printf("No enough memory to allocate!\n");
exit(0);
}
if(head==NULL)
head=p;
else
{
while(pr->next!=NULL)
{
pr=pr->next;
}
pr->next=p;
}
printf("第%d个人的信息\n",i);
printf("Input studentID:");
scanf("%d",&p->studentID);
printf("Input name:");
scanf("%s",p->studentname);
printf("Input score:");
for(j=0;j<m;j++)
{
scanf("%d",&p->score[j]);
}
p->next=NULL;
}
return head;
}
void Calate1(struct student *head,int m,int n)//2
{
int j,sum;
float aver;
struct student *p;
for(j=0;j<m;j++)
{
p=head;
sum=0;
while(p!=NULL)
{
sum=sum+p->score[j];
p=p->next;
}
aver=(float)sum/n;
printf("第%d门课程的总分和平均分为%5d%5.1f\n",j+1,sum,aver);
}
}
void Calate2(struct student *head,int m,int n)//3
{
int sum,i,j;
float averscore;
struct student *p;
for(i=1;i<=n;i++)
{ p=head;
sum=0;
for(j=0;j<m;j++)
{
sum=sum+p->score[j];
}
averscore=(float)sum/m;
p->total=sum;
p->aver=averscore;
p=p->next;
printf("ID:%ld,NAME:%s,TOTAL:%d\n",p->studentID,p->studentname,p->total);
}
}