分析:首先,结构体在函数体内定义,并能将该结构体返回。所以不能定义结构体变量,这样函数结束该结构体变量就销亡了,只能通过malloc申请一个结构体内存,使用结构体指针。其次,该函数无返回值,只能通过参数的形式,将结构体指针返回。最后,通过形参改变实参值的方式有两种:传引用、传指针。不存在对指针变量的引用,所以只能通过传指针的方式,即传指向结构体的指针变量的地址。所以,通过双重指针,实现该功能。
#include <stdio.h>
#include <stdlib.h>
struct Student
{
long age;
};
void fun1(struct Student ** s1){
(*s1) = (struct Student *)malloc(sizeof(struct Student));
(*s1)->age = 27;
}
int main(){
struct Student * s1[1];
fun1(s1);
printf("age = %d\n",(*s1)->age);
free(s1[0]);
return 0;
}