C++小笔记---结构体赋值

来源:互联网 发布:dota臂章数据 编辑:程序博客网 时间:2024/04/29 06:06

struct之间可以直接赋值,并且左右值使用不同的内存空间。

如:

 #include <iostream>
using namespace std;

struct person {
 int age;
 double weight;
};

int main(void) {
 person p1 = { 12, 1.5 };
 cout << p1.age << "   " << p1.weight << endl;

 person p2 = p1;
 cout << p2.age << "   " << p2.weight << endl;

 p2.age = 13;
 p2.weight = 2.0;

 cout << p1.age << "   " << p1.weight << endl;
 cout << p2.age << "   " << p2.weight << endl;

 person *p3 = &p1;
 person p4 = *p3;
 p4.age = 99;
 cout << p4.age << "   " << p4.weight << endl;
 return 0;
}

 

输出结果:

12   1.5
12   1.5
12   1.5
13   2
99   1.5

原创粉丝点击