在一个小端系统中,下面的程序输出?
union{
int i;
unsigned char ch[2];
}Student;
int main()
{
Student student;
student.i=0x1420;
printf("%d %d",student.ch[0],student.ch[1]);
return 0;
}
分析:
1、小端系统:低地址存放低字节
2、不管是大端还是小端系统,a[x] 相对于 a[x+1]总是存放在相对低地址
在一个32位系统中,int占4个字节,在内存中的数据布局:
max
00
00
14 ch[1]
20 ch[0]
low
00
00
14 ch[1]
20 ch[0]
low
0x20 = 32
0x14 = 20
所以输出为:32 20