c++中结构体作为函数参数的使用

结构体虽然和数组一样,都可以存储多个数据项,但是在涉及到函数时,结构变量的行为更接近于一个基本的单值变量,也就是说,与数组不同,结构将其数据组合成单个实体或数据对象,该实体被视为一个整体。函数中参数为结构时,有三种方法:

1.直接将结构作为参数传递,并在需要时作为返回值返回。因此这种方法适用于结构比较小的情况。

例1:

[cpp]  view plain  copy
  1. // travel.cpp -- using structures with functions  
  2. #include <iostream>  
  3. struct travel_time  
  4. {  
  5.     int hours;  
  6.     int mins;  
  7. };  
  8. const int Mins_per_hr = 60;  
  9.   
  10. travel_time sum(travel_time t1, travel_time t2);  
  11. void show_time(travel_time t);  
  12.   
  13. int main()  
  14. {  
  15.     using namespace std;  
  16.     travel_time day1 = {5, 45};    // 5 hrs, 45 min  
  17.     travel_time day2 = {4, 55};    // 4 hrs, 55 min  
  18.   
  19.     travel_time trip = sum(day1, day2);  
  20.     cout << "Two-day total: ";  
  21.     show_time(trip);  
  22.   
  23.     travel_time day3= {4, 32};  
  24.     cout << "Three-day total: ";  
  25.     show_time(sum(trip, day3));  
  26.     // cin.get();  
  27.   
  28.     return 0;  
  29. }  
  30.   
  31. travel_time sum(travel_time t1, travel_time t2) //结构作为函数参数直接传入  
  32. {  
  33.     travel_time total;  
  34.   
  35.     total.mins = (t1.mins + t2.mins) % Mins_per_hr;  
  36.     total.hours = t1.hours + t2.hours +  
  37.                  (t1.mins + t2.mins) / Mins_per_hr;  
  38.     return total;<span style="white-space:pre;">                </span>//结构作为函数结果直接传出  
  39. }  
  40.   
  41. void show_time(travel_time t)  
  42. {  
  43.     using namespace std;  
  44.     cout << t.hours << " hours, "  
  45.          << t.mins << " minutes\n";  
  46. }  


2.传递结构的地址。这种方法适用于结构数据较大时

[cpp]  view plain  copy
  1. // strctptr.cpp -- functions with pointer to structure arguments  
  2. #include <iostream>  
  3. #include <cmath>  
  4.   
  5. // structure templates  
  6. struct polar  
  7. {  
  8.     double distance;      // distance from origin  
  9.     double angle;         // direction from origin  
  10. };  
  11. struct rect  
  12. {  
  13.     double x;             // horizontal distance from origin  
  14.     double y;             // vertical distance from origin  
  15. };  
  16.   
  17. // prototypes  
  18. void rect_to_polar(const rect * pxy, polar * pda);  
  19. void show_polar (const polar * pda);  
  20.   
  21. int main()  
  22. {  
  23.     using namespace std;  
  24.     rect rplace;  
  25.     polar pplace;  
  26.   
  27.     cout << "Enter the x and y values: ";  
  28.     while (cin >> rplace.x >> rplace.y)  
  29.     {  
  30.         rect_to_polar(&rplace, &pplace);    // pass addresses  
  31.         show_polar(&pplace);        // pass address  
  32.         cout << "Next two numbers (q to quit): ";  
  33.     }  
  34.     cout << "Done.\n";  
  35.     return 0;  
  36. }  
  37.   
  38. // show polar coordinates, converting angle to degrees  
  39. void show_polar (const polar * pda)  
  40. {  
  41.     using namespace std;  
  42.     const double Rad_to_deg = 57.29577951;  
  43.   
  44.     cout << "distance = " << pda->distance;  
  45.     cout << ", angle = " << pda->angle * Rad_to_deg;  
  46.     cout << " degrees\n";  
  47. }  
  48.   
  49. // convert rectangular to polar coordinates  
  50. void rect_to_polar(const rect * pxy, polar * pda)<span style="white-space:pre;">    </span>//结构通过指针传入函数  
  51. {  
  52.     using namespace std;  
  53.     pda->distance =  
  54.         sqrt(pxy->x * pxy->x + pxy->y * pxy->y);  
  55.     pda->angle = atan2(pxy->y, pxy->x); <span style="white-space:pre;">        </span>//通过在函数中修改结构的成员  
  56. }  

  • 调用函数时,将结构的地址(&)而不是结构本身传递给它
  • 将形参声明为指向poor的指针,由于函数不应该修改结构,因此使用了const
  • 由于形参是指针而不是结构,因此应该用 间接成员运算符 (-> ),而不是成员运算符(.)
3.按引用传递
参考: c++中引用变量的使用

转自: https://blog.csdn.net/jyt1129/article/details/66973377

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值