C语言 结构体作为参数和返回值使用
2017-07-23 22:21
399 查看
方案一:结构体变量作为参数,进行传值。
编译器需要拷贝,不影响origin value,使用成员操作符(.)直接访问
运行结果:
方案二:结构体指针作为参数,传地址。
编译器不需要拷贝,和main函数采用相同地址。为了不影响origin value,使用const修饰
使用指针指向结构体操作符(->)间接访问
结果和方案一相同。
出现的错误:
编译器需要拷贝,不影响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. 因此,不能对未赋值的临时变量取地址
相关文章推荐
- 《IOS_C语言》高级指针(结构体指针、结构体数组指针、结构体数组指针,作为函数的参数使用、预编译指令)
- C语言中数组作为函数的参数以及返回值的使用简单入门
- 《IOS_作业C语言》高级指针(结构体指针、结构体数组指针、结构体数组指针,作为函数的参数使用、预编译指令)
- C语言_结构体变量指针做函数参数的使用案例
- C#调用C++ 平台调用P/Invoke 结构体--输入输出参数、返回值、返出值、结构体数组作为参数【五】
- Swift -- 函数的使用(函数作为参数、返回值、嵌套函数)
- VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
- [编程] C语言结构体指针作为函数参数
- Shader自学笔记1.2 结构体作为函数的参数和返回值
- C语言 结构体作为函数的参数
- 结构体变量作为函数的参数和返回值 .
- 【网摘】使用结构体作为返回值
- 结构体变量作为函数的参数和返回值
- 例题:把一个超市购物的题做成函数。这个题做成函数没有必要,但可以了解输入参数,输出参数,返回值,函数体,还有结构体,ArryList集合,for循环算结果,以及集合里 .count的使用
- 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
- c语言_函数指针作为函数参数的使用
- 结构体作为参数和返回值原理
- 正确使用指针(指针作为输出型参数)与结构体指针作为函数参数
- C语言--- 高级指针2(结构体指针,数组作为函数参数)
- 使用指针作为函数返回值和参数的问题总结