C語言 結構體作為參數和返回值使用


方案一:結構體變量作為參數,進行傳值。

    編譯器需要拷貝,不影響origin value,使用成員操作符(.)直接訪問

/**********************************************************************
* 版權所有 (C)2017, Wang maochun。
*
* 文件名稱:travel.cpp
* 文件標識:無
* 內容摘要:主要演示結構體作為參數以及返回值 
* 其它說明:"傳值” 
* 當前版本:V1.0
* 作    者:Wang maochun
* 完成日期:2017.7.23
*
**********************************************************************/
#include <iostream>

struct travel_time
{
    int hours;
    int mins;
};

const int Mins_per_hr = 60;

travel_time sum(travel_time t1,travel_time t2);
void show_time(travel_time t);

int main()
{
    using namespace std;
    travel_time day1 = {5,45};  //5 hours 45 minutes
    travel_time day2 = {4,55};  //4 housr 55 minutes
    
    travel_time trip = sum(day1,day2);
    cout << "Two-day total:";
    show_time(trip);
    
    travel_time day3 = {4,32};
    cout << "Three-day total:";
    show_time(sum(trip,day3)); 
    
    return 0;
}

travel_time sum(travel_time t1,travel_time t2)
{
    travel_time total;
    
    total.mins = (t1.mins + t2.mins) % Mins_per_hr;
    total.hours = (t1.hours + t2.hours) + (t1.mins + t2.mins) / Mins_per_hr;
    
    return total;
}

void show_time(travel_time t)
{
    using namespace std;
    cout << t.hours << "hours,"
         << t.mins  << "minutes\n";
} 

運行結果:


方案二:結構體指針作為參數,傳地址。

    編譯器不需要拷貝,和main函數采用相同地址。為了不影響origin value,使用const修飾

    使用指針指向結構體操作符(->)間接訪問

/**********************************************************************
* 版權所有 (C)2017, Wang maochun。
*
* 文件名稱:travel.cpp
* 文件標識:無
* 內容摘要:主要演示結構體作為參數以及返回值 
* 其它說明:"傳地址” 
* 當前版本:V1.0
* 作    者:Wang maochun
* 完成日期:2017.7.23
*
**********************************************************************/
#include <iostream>

struct travel_time
{
    int hours;
    int mins;
};

const int Mins_per_hr = 60;

travel_time sum(travel_time* t1,travel_time* t2);
void show_time(travel_time* t);

int main()
{
    using namespace std;
    travel_time day1 = {5,45};  //5 hours 45 minutes
    travel_time day2 = {4,55};  //4 housr 55 minutes
    
    travel_time trip = sum(&day1,&day2);
    cout << "Two-day total:";
    show_time(&trip);
    
    travel_time day3 = {4,32};
    cout << "Three-day total:";
    travel_time trip1 =sum(&trip,&day3);
    show_time(&(trip1)); 
    
    return 0;
}

travel_time sum(travel_time* t1,travel_time* t2)
{
    travel_time total;
    
    total.mins = (t1->mins + t2->mins) % Mins_per_hr;
    total.hours = (t1->hours + t2->hours) + (t1->mins + t2->mins) / Mins_per_hr;
    
    return total;
}

void show_time(travel_time* t)
{
    using namespace std;
    cout << t->hours << "hours,"
         << t->mins  << "minutes\n";
} 

結果和方案一相同。

 

出現的錯誤:

show_time(&(sum(&day1,&day2)));這樣寫時,出現taking address of temporary fpermissive錯誤。
原因是:
Your middle result which is a temporary variable since it will disappear
If you assign the result of sum(&day1,&day2) to a variable then it will no longer be a temporary and you can then take the address of it.
因此,不能對未賦值的臨時變量取地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM