c++菜鸟问一个关于传递结构的地址的问题
这是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++不久,所以很菜。
有人帮我吗?