问题遇到的现象和发生背景
题目:有n个结构体变量,内含学生学号、姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息
不知道错在哪
用代码块功能插入代码,请勿粘贴截图
#pragma warning(disable : 4996)
#include<stdio.h>
#include<stdlib.h>
#define N 3
struct Student
{
int num;
char name[20];
float score[3];
double aver;
};
int main()
{
void input(struct Student* p1);
struct Student* max(struct Student* p2);
printf("输入3位学生的信息:\n");
struct Student* p;
p = (struct Student*)malloc(sizeof(struct Student));
if (p == NULL)
{
printf("application failed");
return -1;
}
memset(p, 0, sizeof(struct Student*));
input(p);
print(max(p));
free(p);
p = NULL;
return 0;
}
void input(struct Student* p1)
{
int i;
for (i = 0; i < N; i++,p1++)
{
scnaf("%d %s %f %f %f", &(p1->num), &(p1->name), &(p1->score[0]), &(p1->score[1]), &(p1->score[2]));
p1->aver = (p1->score[0] + p1->score[1] + p1->score[2]) / 3.0;
}
}
struct Student* max(struct Student* p2)
{
int i;
int m = 0;
for (i = 0; i < N; i++)
{
if ((p2 + i)->aver > (p2 + m)->aver)
{
m = i;
}
}
return (p2 + m);
}
void print(struct Student* p3)
{
printf("平均成绩最高的学生是:\n");
printf("学生号:%d\n学生姓名:%s\n各个课程的成绩:%5.2f%5.2f%5.2f\n平均成绩:%3.2f", p3->num, p3->name, p3->score[0], p3->score[1], p3->score[2], p3->aver);
}
运行结果及报错内容
struct Student* max(struct Student* p2)函数报错