union的赋值及小端形式
union的赋值及小端模式
系统是基于小端模式的,看了http://topic.****.net/u/20120920/21/6a6a1ba7-dc84-49f7-93b9-9a3e39fe837a.html?seed=1907534755&r=79721416,还是不明白,明明是给student.i赋值:
而char ch[2]并没有被赋值,怎么打印输出student.ch[0],student.ch[1]的值呢
------解决方案--------------------
搞清楚union的原理.
------解决方案--------------------
------解决方案--------------------
联合体是共用存储空间的。
先理解意义再使用吧。
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
楼主,一样的地址,指向的值怎么可能不一样?慢慢想想就明白了。
------解决方案--------------------
union 中的成员是共用存储空间,这一点必须了解,
int student.i=0x1420; 又是小端模式,所以存储顺序: 0x20 0x14
然后student.ch[]是char型的 占一个字节 先后对应 0x20 0x14 十进制即; 32 20
------解决方案--------------------
他们公用同一个存储空间。
- C/C++ code
#include <stdio.h> union{ int i; unsigned char ch[2]; }student; int main() { //union Student student; student.i = 0x1420; printf("%d %d",student.ch[0],student.ch[1]); return 0; }
系统是基于小端模式的,看了http://topic.****.net/u/20120920/21/6a6a1ba7-dc84-49f7-93b9-9a3e39fe837a.html?seed=1907534755&r=79721416,还是不明白,明明是给student.i赋值:
- C/C++ code
student.i = 0x1420;
而char ch[2]并没有被赋值,怎么打印输出student.ch[0],student.ch[1]的值呢
------解决方案--------------------
搞清楚union的原理.
------解决方案--------------------
------解决方案--------------------
联合体是共用存储空间的。
先理解意义再使用吧。
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
楼主,一样的地址,指向的值怎么可能不一样?慢慢想想就明白了。
------解决方案--------------------
union 中的成员是共用存储空间,这一点必须了解,
int student.i=0x1420; 又是小端模式,所以存储顺序: 0x20 0x14
然后student.ch[]是char型的 占一个字节 先后对应 0x20 0x14 十进制即; 32 20
------解决方案--------------------
他们公用同一个存储空间。