我是 C 的新手,一直在尝试运行这个简单的代码,它使用指向结构的指针来计算输入的平均成绩。进入数学成绩后,程序抛出错误并停止。我究竟做错了什么。这也是我在这里的第一篇文章,所以请多多包涵。谢谢!
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *name;
int mathGrade,scienceGrade,historyGrade,englishGrade;
}reportCard;
void average(reportCard *rc)
{
int avg = (rc->mathGrade +rc->scienceGrade+rc->historyGrade+rc->englishGrade)/4;
printf("The overall grade of %s is: %i ",rc->name, avg);
}
int main()
{
reportCard rc;
printf("Enter the Students Last name: ");
char studentName[20];
scanf("%s", studentName);
rc.name=studentName;
printf("Math Grade: \n");
scanf("%i", rc.mathGrade);
printf("Science Grade: \n");
scanf("%i", rc.scienceGrade);
printf("History Grade: \n");
scanf("%i", rc.historyGrade);
printf("English Grade: \n");
scanf("%i", rc.englishGrade);
average(&rc);
return 0;
}