结构体变量作为函数参数

版权声明:版权归Ordinarv所有 https://blog.csdn.net/ordinarv/article/details/82153920

推荐使用引用的方式。因为传递的是变量的地址,因而效率较高,并且可读性也高

#include <iostream>
#include <string>
using namespace std;
struct Student
{
   int num;
   string name;
   float score[3];
}stu={12345,"Li Li",67.5,89,78.5};

void print(Student &stud)//函数定义,形参为结构体Student变量的引用
{
   cout<<stud.num<<" "<<stud.name<<" "<<stud.score[0]
   <<" " <<stud.score[1]<<" "<<stud.score[2]<<endl;
}
int main( )
{
   print(stu);//实参为结构体Student变量
   return 0;
}

另外还有两种方法但缺点都比较明显

参考:https://www.cnblogs.com/wyuzl/p/6248952.html

猜你喜欢

转载自blog.csdn.net/ordinarv/article/details/82153920