C++中传递结构体的问题
程序如下:#include<iostream>structbox{charmaker[40];floatheight;floatwidth;floatlength;flo...
程序如下:
#include <iostream>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void pass_by_value(box a, box b);
int main()
{
using namespace std;
box b1;
cout << "Please input the maker of the box: ";
cin >> b1.maker;
cout << "Please input the height of the box: ";
cin >> b1.height;
cout << "Please input the width of the box: ";
cin >> b1.width;
cout << "Please input the length of the box: ";
cin >> b1.length;
cout << "Please input the volume of the box: ";
cin >> b1.volume;
box b2 = {'o', 0, 0, 0, 0};
pass_by_value(b1, b2);
cout << b2.maker << endl;
cout << b2.height << endl;
cout << b2.width << endl;
cout << b2.length << endl;
cout << b2.volume << endl;
return 0;
}
void pass_by_value(box a, box b)
{
strcpy(b.maker, a.maker);
b.height = a.height;
b.width = a.width;
b.volume = a.volume;
}
但是最后输出的结果仍然是原来b2的值,而不是b1的值,请问这是为什么? 展开
#include <iostream>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void pass_by_value(box a, box b);
int main()
{
using namespace std;
box b1;
cout << "Please input the maker of the box: ";
cin >> b1.maker;
cout << "Please input the height of the box: ";
cin >> b1.height;
cout << "Please input the width of the box: ";
cin >> b1.width;
cout << "Please input the length of the box: ";
cin >> b1.length;
cout << "Please input the volume of the box: ";
cin >> b1.volume;
box b2 = {'o', 0, 0, 0, 0};
pass_by_value(b1, b2);
cout << b2.maker << endl;
cout << b2.height << endl;
cout << b2.width << endl;
cout << b2.length << endl;
cout << b2.volume << endl;
return 0;
}
void pass_by_value(box a, box b)
{
strcpy(b.maker, a.maker);
b.height = a.height;
b.width = a.width;
b.volume = a.volume;
}
但是最后输出的结果仍然是原来b2的值,而不是b1的值,请问这是为什么? 展开
2个回答
展开全部
函数传进去是只是形参,不会改变原结构的
你可以用指针,修改后的代码如下:
#include <iostream>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void pass_by_value(box *a, box *b); // 参数改为结构指针
int main()
{
using namespace std;
box b1;
cout << "Please input the maker of the box: ";
cin >> b1.maker;
cout << "Please input the height of the box: ";
cin >> b1.height;
cout << "Please input the width of the box: ";
cin >> b1.width;
cout << "Please input the length of the box: ";
cin >> b1.length;
cout << "Please input the volume of the box: ";
cin >> b1.volume;
box b2 = {'o', 0, 0, 0, 0};
pass_by_value(&b1, &b2); // 调用函数时要用&取地址作为参数
cout << b2.maker << endl;
cout << b2.height << endl;
cout << b2.width << endl;
cout << b2.length << endl;
cout << b2.volume << endl;
return 0;
}
void pass_by_value(box *a, box *b)
{
strcpy(b->maker, a->maker);
b->height = a->height;
b->width = a->width;
b->volume = a->volume;
}
你可以用指针,修改后的代码如下:
#include <iostream>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void pass_by_value(box *a, box *b); // 参数改为结构指针
int main()
{
using namespace std;
box b1;
cout << "Please input the maker of the box: ";
cin >> b1.maker;
cout << "Please input the height of the box: ";
cin >> b1.height;
cout << "Please input the width of the box: ";
cin >> b1.width;
cout << "Please input the length of the box: ";
cin >> b1.length;
cout << "Please input the volume of the box: ";
cin >> b1.volume;
box b2 = {'o', 0, 0, 0, 0};
pass_by_value(&b1, &b2); // 调用函数时要用&取地址作为参数
cout << b2.maker << endl;
cout << b2.height << endl;
cout << b2.width << endl;
cout << b2.length << endl;
cout << b2.volume << endl;
return 0;
}
void pass_by_value(box *a, box *b)
{
strcpy(b->maker, a->maker);
b->height = a->height;
b->width = a->width;
b->volume = a->volume;
}
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询