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++不久,所以很菜。
有人帮我吗?
/*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++不久,所以很菜。
有人帮我吗?
...全文
相关推荐
菜鸟的文件
是没有用到过的源代码
C++菜鸟总结
初学C++,原创笔记
C++初级菜鸟入门课件简单易学
C++课件从入门到精通 适合初次接触C++的人 快来下载吧!
菜鸟专用c++重载时钟
说了,是在菜鸟专用,对菜鸟理解c++的函数多态性重载有好处的代码
菜鸟关于C++异常的一些思考
近在C++项目中用异常,学习了相关的一些知识,有一些总结,希望能得到大家斧正。 C++之父Bjarne Stroustrup在《The C++ Programming Language》中讲到:一个库的作者可以检测出发生了运行时错误,但一般不知道怎样去处理它们(因为和用户具体的应用有关);另一方面,库的用户知道怎样处理这些错误,但却无法检查它们何时发生(如果能检测,可以再用户的代码里处理了,不用留给库去发现).Bjarne Stroustrup说:提供异常的基本目的是为了处理上面的问题。基本思想是:让一个函数在发现了自己无法处理的错误时抛出(throw)一个异常,然后它的(直接或者间接)


