注册 登录
编程论坛 C语言论坛

结构体函数,怎么在函数中返回结构体类型的变量

宇童鞋 发布于 2019-12-23 10:02, 2216 次点击
题目描述】
 编写函数fun,将形参std所指结构体数组中年龄最大者的数据作为函数值返回,并在主函数中输出。(仅提交函数即可)
 函数接口定义: struct student fun(struct student std[], int n); 其中 std 和 n 都是用户传入的参数。 函数fun的功能是将含有 n 个人的形参 std 所指结构体数组中年龄最大者的数据作为函数值返回。
 【前置信息】
 #include <stdio.h>
struct student {char name[10]; int age; };   
//   *********在此处编写你的函数**********   
 struct student fun(struct student std[], int n);
 int main()
 {
struct student std[5]={"aaa",17,"bbb",16,"ccc",18,"ddd",17,"eee",15 };
struct student max;
max=fun(std,5);
printf("The result:\n");
printf("Name : %s, Age : %d\n", max.name,max.age);
return 0;
}
【输入】
 无
【输出】
The result: Name : ccc, Age : 18
2 回复
#2
rjsp2019-12-23 10:12
若是第0个,你就 return std[0];若是第1个,你就 return std[1];……

我疑惑的是,这与“return 结构体类型的变量”没有任何区别,你为什么要特意问一下?
#3
宇童鞋2019-12-23 10:56
回复 2楼 rjsp
谢谢大佬,刚学结构体,有点懵
1