The C language struct is used as a parameter and a return value

Source: Internet
Author: User

Scenario One: The struct variable is used as a parameter to pass the value.

The compiler needs to copy without affecting Origin value, using the member operator (.). Direct access

/*********************************************************************** All rights reserved (C), Wang Maochun. * * File name: travel.cpp* file ID: None * Content Summary: Main demo struct as parameter and return value * Other Description: "Pass Value" * Current version: v1.0* Author: Wang maochun* finish Date: 2017.7.23************ ***********************************************************/#include<iostream>structtravel_time{inthours; intmins;};Const intMINS_PER_HR = -; Travel_time sum (travel_time t1,travel_time T2);voidshow_time (Travel_time t);intMain () {using namespacestd; Travel_time Day1= {5, $};//5 hours minutesTravel_time Day2 = {4, -};//4 housr minutesTravel_time Trip=sum (day1,day2); cout<<"two-day Total:";        Show_time (Trip); Travel_time Day3= {4, +}; 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; returnTotal ;}voidshow_time (Travel_time t) {using namespacestd; cout<< t.hours <<"hours,"<< T.mins <<"minutes\n";} 

Operation Result:

Scenario Two: The struct pointer is used as a parameter to pass the address.

The compiler does not need a copy, and the main function takes the same address. To not affect Origin value, use the const modifier

Use pointers to structural gymnastics (-) indirect access

/*********************************************************************** All rights reserved (C), Wang Maochun. * * File name: travel.cpp* file ID: None * Content Summary: Main demo struct as parameter and return value * Other Description: "Address" * Current version: v1.0* Author: Wang maochun* finish Date: 2017.7.23*********** ************************************************************/#include<iostream>structtravel_time{inthours; intmins;};Const intMINS_PER_HR = -; Travel_time sum (travel_time* t1,travel_time*T2);voidShow_time (travel_time*t);intMain () {using namespacestd; Travel_time Day1= {5, $};//5 hours minutesTravel_time Day2 = {4, -};//4 housr minutesTravel_time Trip= SUM (&day1,&day2); cout<<"two-day Total:"; Show_time (&Trip ); Travel_time Day3= {4, +}; 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; returnTotal ;}voidShow_time (travel_time*t) {    using namespacestd; cout<< t->hours <<"hours,"<< T->mins <<"minutes\n";} 

The result is the same as the scenario one.

The error that occurred:

show_time (sum(&day1,&day2)); Taking address of temporary fpermissive error occurs when this is written.
The reasons are:
Your Middle result which is a temporary variable since it'll disappear
If you assign the result of sum (&day1,&day2) to a variable then it'll no longer be a temporary and you can then Take the address of it.
Therefore, you cannot take an address on a temporary variable that is not assigned a value

The C language struct is used as a parameter and a return value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.