【中兴面试题】
#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