联合体
- //example
- #include<stdio.h>
- union var{
- long int l;
- int i;
- }; /*定义一个联合体,与结构体较类似*/
- main(){
- union var v;
- v.l = 5;
- printf("v.l is %d\n",v.i);
- v.i = 6;
- printf("now v.l is %ld! the address is %p\n",v.l,&v.l);
- printf("now v.i is %d! the address is %p\n",v.i,&v.i);
- }
- 结果:
- v.l is 5
- now v.l is 6! the address is 0xbfad1e2c
- now v.i is 6! the address is 0xbfad1e2c
- 从以上结果可以看出,联合体与结构体的不同之处是联合体中变量用的是同一个内存地址,而结构体中的变量的地址是不同的。
posted on 2018-10-10 20:52 xiegangqingnian 阅读(149) 评论(0) 编辑 收藏 举报