测试代码:
1 #include <stdio.h>
2 #define TEST_ULONG 1
3 #define TEST_STRUCT 1
4 #define TEST_BIT 1
5 typedef unsigned long ULONG64;
6 typedef unsigned int ULONG32;
7 typedef unsigned char UCHAR;
8 typedef unsigned short USHORT;
9 typedef struct stLong32
10 {
11 USHORT usPara1;
12 UCHAR ucPara2;
13 UCHAR ucPara3;
14 }ST_LONG_32;
15 typedef struct stLongBit
16 {
17 ULONG32 ulPara1:16;
18 ULONG32 ulPara2:8;
19 ULONG32 ulPara3:8;
20 }ST_LONG_BIT;
21 int main(int argc, char *argv[])
22 {
23 ULONG32 ulLong32;
24 ST_LONG_32 stLong32;
25 ST_LONG_BIT stLongBit;
26 UCHAR *pucPtr1, *pucPtr2, *pucPtr3, *pucPtr4;
27 printf("ULONG: %#x, STLONG: %#x, STBIT: %#x\n", &ulLong32, \
28 &stLong32, &stLongBit);
29 #if TEST_ULONG
30 ulLong32 = 0x01020304;
31 pucPtr1 = (UCHAR*)(&ulLong32);
32 pucPtr2 = (UCHAR*)(&ulLong32) + 1;
33 pucPtr3 = (UCHAR*)(&ulLong32) + 2;
34 pucPtr4 = (UCHAR*)(&ulLong32) + 3;
35 printf("ULONG: %#x %#x %#x %#x\n", pucPtr1, pucPtr2, pucPtr3, pucPtr4);
36 printf("ULONG: %#x %#x %#x %#x\n", *pucPtr1, *pucPtr2, *pucPtr3, *pucPtr4);
37 #endif
38 #if TEST_STRUCT
39 stLong32 = *(ST_LONG_32*)(&ulLong32);
40 printf("STLONG: %#x %#x %#x\n", &stLong32.usPara1, &stLong32.ucPara2, &stLong32.ucPara3);
41 printf("STLONG: %#x %#x %#x\n", stLong32.usPara1, stLong32.ucPara2, stLong32.ucPara3);
42 #endif
43 #if TEST_BIT
44 stLongBit = *(ST_LONG_BIT*)(&ulLong32);
45 printf("STBIT: %#x %#x %#x\n", stLongBit.ulPara1, stLongBit.ulPara2, stLongBit.ulPara3);
46 #endif
47 return 0;
48 }
结果输出:
0 root@debian:/home/...# ./ddr
1 ULONG: 0xd0ee672c, STLONG: 0xd0ee6720, STBIT: 0xd0ee6710
2 ULONG: 0xd0ee672c 0xd0ee672d 0xd0ee672e 0xd0ee672f
3 ULONG: 0x4 0x3 0x2 0x1
4 STLONG: 0xd0ee6720 0xd0ee6722 0xd0ee6723
5 STLONG: 0x304 0x2 0x1
6 STBIT: 0x304 0x2 0x1
输出结果分析:
查看源代码和第三行输出,可以发现,运行的机器应该是小端存储的
对比第一行和第二行输出,可以发现指针在内存段中的位置
对比第五行和第三行输出,可以发现...
原文:http://www.cnblogs.com/kellis/p/5071571.html