|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 1 #include <stdio.h>
- 2
- 3 int main()
- 4 {
- 5 int a = 0x12345678;
- 6 unsigned char *p = (unsigned char*)&a;
- 7 if (*p == 0x12)
- 8 printf("您的机器采用大端字节序。\n");
- 9 else
- 10 printf("您的机器采用小端字节序。\n");
- 11
- 12 printf("0x12345678 在内存中依次存放为:0x%2x 0x%2x 0x%2x 0x%2x\n", p[0], p[1], p[2], p[3]);
- 13 printf("0x12345678 在内存中依次存放为:0x%2x 0x%2x 0x%2x 0x%2x\n", *(p++), *(p++), *(p++), *(p++));
- 14
- 15 return 0;
- 16 }
复制代码 运行结果如图:
为什么会出现相反的打印结果呢?求大神指教!
不能这样连着写
- #include <stdio.h>
- int main()
- {
- int a = 0x12345678;
- unsigned char *p = (unsigned char*)&a;
- if (*p == 0x12)
- printf("您的机器采用大端字节序。\n");
- else
- printf("您的机器采用小端字节序。\n");
- printf("0x12345678 在内存中依次存放为:0x%2x 0x%2x 0x%2x 0x%2x\n", p[0], p[1], p[2], p[3]);
- //printf("0x12345678 在内存中依次存放为:0x%2x 0x%2x 0x%2x 0x%2x\n", *(p++), *(p++), *(p++), *(p++));
- printf("0x12345678 在内存中依次存放为:");
- printf("0x%2x ", *p++);
- printf("0x%2x ", *p++);
- printf("0x%2x ", *p++);
- printf("0x%2x ", *p++);
- puts("");
- return 0;
- }
复制代码
|
-
|