关于联合体的问题!
union a
{
int b;
struct k
{
int x:2;
int y:3;
int z:3;
};
k c;
};
void main()
{
a a;
a.b = 100;
cout < < a.c.x < < ", " < < a.c.y < < ", " < < a.c.z < < endl;
}
输出为什么是:0,1,3
[解决办法]
从调试结果来看,编译器在调试的时候给x,y,z赋的值就是0,1,3。所以打印出来的结果也就是0,1,3。如果要让其打印结果为2,3,3的话可以这样写。
union a
{
int b;
struct k
{
int x;
int y;
int z;
};
k c;
};
void main( )
{
a a;
a.b = 100;
a.c.x = 2;
a.c.y = 3;
a.c.z = 3;
cout < < a.c.x < < ", " < < a.c.y < < ", " < < a.c.z < < endl;
}
[解决办法]
自己看吧!我也上了一课!
http://dev.yesky.com/374/2645874.shtml