将一个结构体变量中的数据传递给另一个函数,有下列3种方法:
- 用结构体变量名作参数。一般较少用这种方法。
- 用指向结构体变量的指针作实参,将结构体变量的地址传给形参。
- 用结构体变量的引用变量作函数参数。
下面通过一个简单的例子来说明,并对它们进行比较。
有一个结构体变量stu,内含学生学号、姓名和3门课的成绩。要求在main函数中为各成员赋值,在另一函数print中将它们的值输出。
1.结构体变量作函数参数
/*************************************************************************
> File Name: test.c
> Author: zhengdongqi
> Mail:
> Created Time: 一 4/15 19:36:06 2019
************************************************************************/
#include <stdio.h>
#include <string.h>
struct Student {
int num;
char *name;
float score[3];
};
void print(struct Student st) {
printf("%d, %s, %0.1f %0.1f %0.1f\n", st.num, st.name, st.score[0], st.score[1