用c语言写出定义一个类型struct student, 他有下列三个数据项:姓名(name)char类型【10】学号(number)char类型【10】成绩(score)现有如下数据zhangliang 19040301 87 lihong 18040302 92 yanfeng 20040303 83将他们存入struct student数组中请编写函数fun1(),函数的功能是找出成绩最高的学生记录,通过形参返回主函数
时间: 2023-11-27 08:53:54 浏览: 25
以下是用C语言实现的代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
char name[10];
char number[10];
int score;
};
void fun1(struct student stu[], int n, struct student *max) {
int i;
*max = stu[0]; // 初始化成绩最高的学生为第一个学生
for(i = 1; i < n; i++) {
if(stu[i].score > (*max).score) {
*max = stu[i]; // 如果找到成绩更高的学生,则更新max指针指向的对象
}
}
}
int main() {
struct student stu[3] = {
{"zhangliang", "19040301", 87},
{"lihong", "18040302", 92},
{"yanfeng", "20040303", 83}
};
struct student maxstu;
fun1(stu, 3, &maxstu);
printf("The student with the highest score is: %s, %s, %d\n", maxstu.name, maxstu.number, maxstu.score);
return 0;
}
```
在上面的代码中,定义了一个结构体类型`struct student`,它包含了三个数据项:姓名、学号和成绩。在`main`函数中,定义了一个包含3个学生记录的数组`stu`,并且调用`fun1`函数来寻找成绩最高的学生记录。`fun1`函数通过形参返回了成绩最高的学生记录,即使用指针`max`来存储成绩最高的学生记录对象的地址。在`fun1`函数中,使用循环和条件判断来寻找成绩最高的学生记录。最后,在`main`函数中,打印出成绩最高的学生记录的姓名、学号和成绩。