c++菜鸟问一个关于传递结构的地址的问题

digiworm digiworm 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这是c++primer plus 4th 上的一道练习题,应该是7_3.
/*Here is a structure template:

struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};

a.Write a function that passes a box structure by value and that displays
the value of each member.
b.Write a function that passes the address of a box structure and that sets
the volume member to the product of the other three dimensions.
c.Write a simple program that uses these two functions */

#include<iostream>
using namespace std;

struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};

void showbox(box b);
void setbox(box* pb);

int main()
{
box digiworm={"Digiworm",3,4,5};
setbox(&digiworm);
showbox(digiworm);
return 0;
}

void showbox(box b)
{
cout<<"Box maker: "<<b.maker<<endl;
cout<<"height: "<<b.height<<endl;
cout<<"width: "<<b.width<<endl;
cout<<"length: "<<b.length<<endl;
cout<<"volume: "<<b.volume<<endl;
}

void setbox(box* pb)
{
pb->volume=pb->height*pb->width*pb->length;
}

我的问题是:
setbox函数并没有返回volume的值,它是如何让后面的showbox知道volume的值的。
我也知道这是指针的妙用,但还是不太理解。
我刚学c++不久,所以很菜。
有人帮我吗?
...全文
评论 11
收藏 已收藏
分享
返回顶部
digiworm
C++ 语言 65,212 成员 250,513 内容