c语言结构体作为形参是否加struct_C语言 结构体作为参数和返回值使用

https://www.cnblogs.com/shuqingstudy/p/7226413.html

方案一:结构体变量作为参数,进行传值。

编译器需要拷贝,不影响origin value,使用成员操作符(.)直接访问

174073154_1_20191025110854691.gif

/**********************************************************************

* 版权所有 (C)2017, Wang maochun。

*

* 文件名称:travel.cpp

* 文件标识:无

* 内容摘要:主要演示结构体作为参数以及返回值

* 其它说明:"传值”

* 当前版本:V1.0

* 作 者:Wang maochun

* 完成日期:2017.7.23

*

**********************************************************************/

#include

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";

}

174073154_1_20191025110854691.gif

运行结果:

174073154_2_20191025110854769.png

方案二:结构体指针作为参数,传地址。

编译器不需要拷贝,和main函数采用相同地址。为了不影响origin value,使用const修饰

使用指针指向结构体操作符(->)间接访问

174073154_1_20191025110854691.gif

/**********************************************************************

* 版权所有 (C)2017, Wang maochun。

*

* 文件名称:travel.cpp

* 文件标识:无

* 内容摘要:主要演示结构体作为参数以及返回值

* 其它说明:"传地址”

* 当前版本:V1.0

* 作 者:Wang maochun

* 完成日期:2017.7.23

*

**********************************************************************/

#include

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";

}

174073154_1_20191025110854691.gif

结果和方案一相同。

出现的错误:

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.

因此,不能对未赋值的临时变量取地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们需要在函数中操作结构体类型的数据时,可以将结构体类型作为函数的形参,这样在函数内部就可以对结构体类型数据进行操作。 如果结构体中嵌套了其他结构体,我们可以将嵌套的结构体类型作为结构体类型的成员之一,并在函数参数中传递该结构体类型。 例如,定义一个名为 `Student` 的结构体类型,其中嵌套了一个名为 `Birthday` 的结构体类型,代码如下: ```c #include <stdio.h> struct Birthday { int year; int month; int day; }; struct Student { char name[20]; int age; struct Birthday birthday; }; void printStudent(struct Student stu) { printf("Name: %s\n", stu.name); printf("Age: %d\n", stu.age); printf("Birthday: %d-%02d-%02d\n", stu.birthday.year, stu.birthday.month, stu.birthday.day); } int main() { struct Student stu = {"Tom", 18, {2000, 1, 1}}; printStudent(stu); return 0; } ``` 在上面的示例中,我们定义了一个名为 `Birthday` 的结构体类型和一个名为 `Student` 的结构体类型,`Student` 结构体中嵌套了 `Birthday` 结构体类型。在 `printStudent` 函数中,我们将 `Student` 结构体类型作为参数传递,可以直接通过 `stu.birthday` 访问嵌套的 `Birthday` 结构体类型的成员变量。 当然,我们也可以将 `Student` 结构体类型的指针作为函数参数,代码如下: ```c void printStudent(struct Student *pStu) { printf("Name: %s\n", pStu->name); printf("Age: %d\n", pStu->age); printf("Birthday: %d-%02d-%02d\n", pStu->birthday.year, pStu->birthday.month, pStu->birthday.day); } int main() { struct Student stu = {"Tom", 18, {2000, 1, 1}}; printStudent(&stu); return 0; } ``` 注意,在函数中访问结构体类型的成员变量时,可以使用 `.` 运算符或 `->` 运算符,前者用于结构体类型变量,后者用于结构体类型指针。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值