看到论坛有人问一道题,说是曙光面试题。
http://topic.csdn.net/u/20111210/23/73048ed6-68d6-4678-a2b4-1d21c5b62c29.html
#include <stdio.h>
void main()
{
int a = 0x12345678;
int b = 0x567890AB;
int c = 0x7890ABCD;
char * p = (char*)&a;
char * q = p + 2;
int x = *(int*)q;
printf("x=%X",x);
}
这段代码主要的问题就是把int*转换成char*之后取到的数值。
假设目标主机是小端序列(little-endian),常用的x86,x64都是这种类型的。
那么:0x12345678在内存中存储结构应该是 0x78,0x56,0x34,0x12.
这个很容易测试的,打开调试器,打个断点看内存就知道。
</