union详解

[flydream@flydream ThinkingInC++]$ cat union.cpp -n
     1    #include <iostream>
     2    
     3    using namespace std;
     4    
     5    union Packed
     6    {
     7        char i;
     8        short j;
     9        int k;
    10        long l;
    11        float f;
    12        double d;
    13    };
    14    
    15    int main(int argc, char **argv)
    16    {    
    17        cout << "sizeof(Packed) = " << sizeof(Packed) << endl;
    18        Packed x;
    19        x.i = 'c';
    20        cout << x.i << endl;
    21        x.d = 3.14159;
    22        cout << x.d << endl;
    23        x.f = 2.222;
    24        cout << x.i << endl; /* 作为一个整形输出无意义 */
    25    
    26        cout << "&x.i = " << &x.i << endl; /* 这里的输出乱码不知道为什么 */    

    27        cout << "&x.j = " << &x. j<< endl;    
    28        cout << "&x.k = " << &x.k << endl;    
    29        cout << "&x.l = " << &x. l<< endl;    
    30        cout << "&x.f = " << &x.f << endl;    
    31        cout << "&x.d = " << &x.d << endl;    
    32        return 0;
    33    }
[flydream@flydream ThinkingInC++]$ g++ union.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
sizeof(Packed) = 8
c
3.14159
?   作为一个整形输出无意义
&x.i = ?5@�!    @��
这里的输出乱码不知道为什么
&x.j = 0xbfc069d8
&x.k = 0xbfc069d8
&x.l = 0xbfc069d8
&x.f = 0xbfc069d8
&x.d = 0xbfc069d8
[flydream@flydream ThinkingInC++]$




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值