zoukankan      html  css  js  c++  java
  • 结构体类型数据作为函数参数(三种方法)---转

        将一个结构体变量中的数据传递给另一个函数,有下列3种方法:

    1. 用结构体变量名作参数。一般较少用这种方法。
    2. 用指向结构体变量的指针作实参,将结构体变量的地址传给形参。
    3. 用结构体变量的引用变量作函数参数。

       下面通过一个简单的例子来说明,并对它们进行比较。

      有一个结构体变量stu,内含学生学号、姓名和3门课的成绩。要求在main函数中为各成员赋值,在另一函数print中将它们的值输出。

     1) 用结构体变量作函数参数。

      

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 struct Student//声明结构体类型Student
     5 {
     6    int num;
     7    char name[20];
     8    float score[3];
     9 };
    10 int main( )
    11 {
    12    void print(Student); //函数声明,形参类型为结构体Student
    13    Student stu; //定义结构体变量               
    14    stu.num=12345; //以下5行对结构体变量各成员赋值
    15    stu.name="Li Fung";
    16    stu.score[0]=67.5;
    17    stu.score[1]=89;
    18    stu.score[2]=78.5;
    19    print(stu); //调用print函数,输出stu各成员的值
    20    return 0;
    21 }
    22 void print(Student st)
    23 {
    24    cout<<st.num<<" "<<st.name<<" "<<st.score[0]
    25    <<" " <<st.score[1]<<" "<<st.score[2]<<endl;
    26 }

      2)用指向结构体变量的指针作实参在上面程序的基础上稍作修改即可。

      

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 struct Student
     5 {
     6    int num; string name; //用string类型定义字符串变量
     7    float score[3];
     8 }stu={12345,"Li Fung",67.5,89,78.5}; //定义结构体student变量stu并赋初值
     9 int main( )
    10 {
    11    void print(Student *); //函数声明,形参为指向Student类型数据的指针变量
    12    Student *pt=&stu; //定义基类型为Student的指针变量pt,并指向stu
    13    print(pt); //实参为指向Student类数据的指针变量
    14    return 0;
    15 }
    16 
    17 //定义函数,形参p是基类型为Student的指针变量
    18 void print(Student *p)
    19 {
    20    cout<<p->num<<" "<<p->name<<" "<<p->score[0]<<" " <<
    21    p->score[1]<<" "<<p->score[2]<<endl;
    22 }

      调用print函数时,实参指针变量pt将stu的起始地址传送给形参p(p也是基类型为student的指针变量)。这样形参p也就指向stu

    在print函数中输出p所指向的结构体变量的各个成员值,它们也就是stu的成员值。在main函数中也可以不定义指针变量pt,而在调用print函数时以&stu作为实参,把stu的起始地址传给实参p。

     3) 用结构体变量的引用作函数参数

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 struct Student
     5 {
     6    int num;
     7    string name;
     8    float score[3];
     9 }stu={12345,"Li Li",67.5,89,78.5};
    10 
    11 int main( )
    12 {
    13    void print(Student &);
    14    //函数声明,形参为Student类型变量的引用
    15    print(stu);
    16    //实参为结构体Student变量
    17    return 0;
    18 }
    19 
    20 //函数定义,形参为结构体Student变量的引用
    21 void print(Student &stud)
    22 {
    23    cout<<stud.num<<" "<<stud.name<<" "<<stud.score[0]
    24    <<" " <<stud.score[1]<<" "<<stud.score[2]<<endl;
    25 }

    程序(1)用结构体变量作实参和形参,程序直观易懂,效率是不高的。
    程序(2)采用指针变量作为实参和形参,空间和时间的开销都很小,效率较高。但程序(2)不如程序(1)那样直接。
    程序(3)的实参是结构体Student类型变量,而形参用Student类型的引用,虚实结合时传递的是stu的地址,因而效率较高。它兼有(1)和(2)的优点。

  • 相关阅读:
    Finance_Time-Series-Analysis-with-app-in-R
    Linear_algebra_06_二次型
    Linear_algebra_05_相似对角形
    病理学
    S&p_14_参数的假设检验
    S&p_13_参数区间估计
    Finance_Analysis-of-Financial-Time-Series
    817. Linked List Components
    811. Subdomain Visit Count
    807. Max Increase to Keep City Skyline
  • 原文地址:https://www.cnblogs.com/wyuzl/p/6248952.html
Copyright © 2011-2022 走看看