c++//union//20191222

#include<iostream>
using namespace std;
int main()
{
	struct s1
	{
		char mark;
		long num;
		float score;
	}s1;
	union s2
	{
		char mark;
		long num;
		float score;
	}s2;
	cout << sizeof(s1.mark) << endl;
	cout << sizeof(s1.num) << endl;
	cout << sizeof(s1.score) << endl;
	cout << sizeof(s1) << endl;
	cout << "------------" << endl;
	cout << sizeof(s2.mark) << endl;
	cout << sizeof(s2.num ) << endl;
	cout << sizeof(s2.score) << endl;
	cout << sizeof(s2) << endl;

}

输出

1
4
4
12
------------
1
4
4
4

H:\Project47\Debug\Project47.exe (进程 17036)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...

参考:https://www.cnblogs.com/jeakeven/p/5113508.html

发布了38 篇原创文章 · 获赞 2 · 访问量 1182

猜你喜欢

转载自blog.csdn.net/weixin_44811068/article/details/103653762