分享

(struct)结构体变量作为函数参数调用的方法小结

 薛董_艾瑞 2018-03-26

结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析 

  1. struct stud  
  2. {  
  3.     long int num;  
  4.     float score;  
  5. };  

/*结构体变量作为函数的参数,修改之后的成员值不能返回到主调函数*/

  1. void funvr(struct stud t)  
  2. {  
  3.     t.num=2000101;  
  4.     t.score=71.0;  
  5. }  

/*结构体数组作为函数的参数,修改后的元素的成员值能返回到主调函数*/

  1. void funar(struct stud t[])  
  2. //void funar(struct stud &t)  
  3. {  
  4.     t[0].num=3000101;         /*注意结构体数组元素的成员的引用形式*/  
  5.     t[0].score=81.0;  
  6.     t[1].num=3000102;  
  7.     t[1].score=82.0;  
  8. }  

/*结构体指针变量作为函数的参数,修改后的结构体成员的值能返回到主调函数*/

  1. void funpr(struct stud *t)  
  2. {  
  3.     t->num=4000101;          /*注意通过结构体指针变量引用成员的具体形式*/  
  4.     (*t).score=92.0;  
  5. }  


/*在主函数中分别调用上述函数修改成员值,再验证结果的正确性*/

  1. #include<stdio.h>  
  2.   
  3. struct stud  
  4. {  
  5.     long int num;  
  6.     float score;  
  7. };  
  8.   
  9. void funvr(struct stud t)  
  10. {  
  11.     t.num=2000101;  
  12.     t.score=71.0;  
  13. }  
  14.   
  15. void funar(struct stud t[])  
  16. //void funar(struct stud &t)  
  17. {  
  18.     t[0].num=3000101;         /*注意结构体数组元素的成员的引用形式*/  
  19.     t[0].score=81.0;  
  20.     t[1].num=3000102;  
  21.     t[1].score=82.0;  
  22. }  
  23.   
  24. void funpr(struct stud *t)  
  25. {  
  26.     t->num=4000101;          /*注意通过结构体指针变量引用成员的具体形式*/  
  27.     (*t).score=92.0;  
  28. }  
  29.   
  30. void main()  
  31. {  
  32.     struct stud a[2]={  
  33.                         {1000101,61.0}, {1000102,62.0}  
  34.                      };  
  35.     struct stud b=a[0],*p;  
  36.       
  37.     printf("old b: b.num:%ld\tb.score:%f\n",b.num,b.score);  
  38.     /*显示结构体变量b的成员的原有值*/  
  39.     funvr(b);  
  40.     /*验证第一种情况,观察并分析结果,看结构体变量作为函数参数时,形参结构体变量成员的值的改变能影响实参结构体变量的成员的值, 
  41.       以下为输出调用函数funvr(b)之后的结果值*/  
  42.     printf("call funvr() new b: b.num:%ld\tb.score:%f\n ",b.num,b.score);  
  43.     funpr(&b);            /*将结构体变量的指针对作为函数的参数*/  
  44.     printf("call funpr() new b: b.num:%ld\tb.score:%f\n ",b.num,b.score);  
  45.                          /*输出结构体数组a元素的原来的成员值*/  
  46.     printf("old a[0]:a[0].num:%ld\ta[0].score:%f\n ",a[0].num,a[0].score);  
  47.     printf("old a[1]:a[1].num:%ld\ta[1].score:%f\n ",a[1].num,a[1].score);  
  48.     /*将结构体数组a作为函数的参数,然后再输出其元素的成员的值,已经被修改了*/  
  49.     funar(a);  
  50.     printf(" new a[0]:a[0].num:%ld\ta[0].score:%f\n ",a[0].num,a[0].score);  
  51.     printf("new a[1]:a[1].num:%ld\ta[1].score:%f\n ",a[1].num,a[1].score);  
  52.       
  53. }  

【结构体参数调用归纳】

1)结构体变量作为函数参数[实参与形参]时,形参结构体变量成员值的改变不影响对应的实参构体变量成员值的改变。

2)结构体数组或结构体指针变量作为函数参数[实参与形参]时,形参结构体数组元素[或形参结构体指针变量指向的变量]成员值的改变将影响对应的实参构体数组[或实参结构体指针变量指向的变量]成员值的改变。

3)结构体变量可作为函数的参数,函数可返回一结构体类数据

4)p=&b; 使结构体指针变量p指向结构体变量b的空间。

     p->num:表示通过指针变量引用结构体变量b的成员num

5)p=a;或p=&a[0];将结构体指针变量指向结构体数组a。则:

             ①p->num:表示通过指针变量引用结构体数组元素的成员num的值。

             ②p->num++:表示通过指针变量先引用结构体数组元素的成员num的值,再使该元素的成员num的值加 1,先引用其值然后其加1。

             ③++p->num:表示使指向的元素的成员num的值加1,再引用其值。

6)p=a;或p=&a[0];表示将结构体指针变量p指向结构体数组a。

             ①(p++)->num:表示通过指针变量先引用结构体数组元素  的成员num的值,再使指针变量本身加1,指针变量加1表示使指针变量指向结构体数组的下一个元素。

             ②(++p)->num:先使指针变量本身加1,先使使指针变量指向结构体数组的下一个元素,然后引用指针变量所指向的结构体数组元素的成员num的值。

结构体变量作为函数的形式参数实验总结

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. typedef struct str{  
  6.     int len;  
  7.     char s[0];  
  8.   
  9.     str(int k)  
  10.     {  
  11.         len=k;  
  12.     }  
  13.   
  14. }Str;  
  15.    
  16. void make(Str tmp) // 注意当函数的形参是Str tmp 时,传递的方式是普通的结构体变量传值调用,这里让len的值改变不会影响下面的temp的len  
  17. {  
  18.     tmp.len=2;  
  19. }  
  20.   
  21. void make_ptr(Str &tmp)  // 当函数的形参是Str &tmp,时传递的是结构体的地址,当这里使得tmp.len值改变时,实参temp.len值也会改变  
  22. {  
  23.     tmp.len=3;  
  24. }  
  25.   
  26. void make_ptr2(Str *tmp) // 函数参数是一个Str类型的指针,传地址调用就不用说了,(上面的引用方式是C++的特性之一,C语言不能那样使用)  
  27. {  
  28.     tmp->len=4;  
  29. }  
  30.   
  31.    
  32. int main(int argc, char** argv) {  
  33.       
  34.     struct str temp(1);  
  35.     printf("temp=%d\n",temp);  
  36.       
  37.     make(temp);  
  38.     printf("temp=%d\n",temp);  
  39.       
  40.     make_ptr(temp);  
  41.     printf("temp=%d\n",temp);  
  42.       
  43.     return 0;  
  44. }  

结构体变量作为函数参数传递的3种方法

将一个结构体变量中的数据传递给另一个函数,有下列3种方法:
用结构体变量名作参数。一般较少用这种方法。
用指向结构体变量的指针作实参,将结构体变量的地址传给形参。
用结构体变量的引用变量作函数参数。


下面通过一个简单的例子来说明,并对它们进行比较。
【例7.5】有一个结构体变量stu,内含学生学号、姓名和3门课的成绩。要求在main函数中为各成员赋值,在另一函数print中将它们的值输出。

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

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


调用print函数时,实参指针变量pt将stu的起始地址传送给形参p(p也是基类型为student的指针变量)。这样形参p也就指向stu,见图7.10。
在print函数中输出p所指向的结构体变量的各个成员值,它们也就是stu的成员值。在main函数中也可以不定义指针变量pt,而在调用print函数时以&stu作为实参,把stu的起始地址传给实参p。


图7.10

  1. 3) 用结构体变量的引用作函数参数  
  2.   
  3. #include <iostream>  
  4. #include <string>  
  5. using namespace std;  
  6. struct Student  
  7. {  
  8.    int num;  
  9.    string name;  
  10.    float score[3];  
  11. }stu={12345,"Li Li",67.5,89,78.5};  
  12. int main( )  
  13. {  
  14.    void print(Student &);  
  15.    //函数声明,形参为Student类型变量的引用  
  16.    print(stu);  
  17.    //实参为结构体Student变量  
  18.    return 0;  
  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)的优点。

引用变量主要用作函数参数,它可以提高效率,而且保持程序良好的可读性(引用'&'是C++的新特性)。在本例中用了string方法定义字符串变量,在某些C++系统中目前不能运行这些程序,读者可以修改程序,使之能在自己所用的系统中运行。


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多