网上的有一些说的不清楚,重新对网上程序修改,得出结论
#include<iostream>
union{int i;
char x[4];
}a;
int main()
{
a.x[0] = 10;
a.x[1] = 1;
a.x[2] = 0;
std::cout << sizeof(a) << std::endl << a.i << std::endl;
// printf("%d\n", a.i);
system("pause");
return 0;
}
//输出为4 266
原因为系统为a分配4字节即32位初始为0000000....共32...0,赋值后0000 0000 0000 0000 0000 00001 0000 1010,转为10进制为266