union赋值及小端系统

来源:互联网 发布:火车票代理软件 编辑:程序博客网 时间:2024/05/18 16:37

【中兴面试题】

#include<stdio.h>#include<stdlib.h>typedef union {int i;unsigned char ch[2];} Student;int main(int argc, char const *argv[]){Student student;student.i = 0X1420;printf("%d %d \n",student.ch[0], student.ch[1] );return 0;}
输出结果?(答案:32 20)

【分析】

union 中的成员是共用存储空间,这一点必须了解。而struct中成员都分配了自己的存储空间。
int student.i=0x1420,又是采用小端系统,所以它的存储顺序: 0x20 0x14  
然后student.ch[]是char型的 占一个字节 先后对应 0x20 0x14 十进制即:32 20 

0 0
原创粉丝点击