在C中初始化一个整数数组,它在x86下的RAM中看起来像什么?

时间:2012-03-22 23:40:49

标签: c sockets constants ipv6 endianness

u_int32_t ip6_address[1][4] = { {0x00000001, 0x0, 0x0, 0x12345678} };
  1. 当在x86 PC CPU上运行时,上述内容如何逐字节地逐字节转换?

2 个答案:

答案 0 :(得分:5)

有时候看起来最简单:

$ cat example.c 
#include <stdint.h>
uint32_t ip6_address[1][4] = { {0x00000001, 0x0, 0x0, 0x12345678} };
$ make example.o
clang -Wall -Wextra -pedantic   -c -o example.o example.c
$ otool -d example.o 
example.o:
(__DATA,__data) section
0000000000000000    01 00 00 00 00 00 00 00 00 00 00 00 78 56 34 12 

你可以为你的第二个例子做一些类似的事情。

答案 1 :(得分:1)

您可以通过unsigned char *

检查任何内存区域
void dump(void *address, size_t bytes) {
    unsigned char *p = address;
    while (bytes--) printf("%02X ", *p++);
    puts("");
}