/* +------------------------------------------------------------------------+ | 小端存储: 低地址存放低位数据; | | 例: int型数 0x12 34 56 78 | | | | | | 高位 低位 | | | | ------------------------------------------ | | 1 | | --------------------- | | &a -> 0x0022FF54 -> 12345678 | | --------------------- | | &a[0] -> 0x0022FF54 -> 78 | | &a[1] -> 0x0022FF55 -> 56 | | &a[2] -> 0x0022FF56 -> 34 | | &a[3] -> 0x0022FF57 -> 12 | +------------------------------------------------------------------------+ */ #include <stdbool.h> void test() { int a = 0x12345678; printf("&a \t-> 0x%p\t-> %x\n" "---------------------\n" "&a[0]\t-> 0x%p\t-> %x\n" "&a[1]\t-> 0x%p\t-> %x\n" "&a[2]\t-> 0x%p\t-> %x\n" "&a[3]\t-> 0x%p\t-> %x\n", &a, a, (char*)&a + 0, *((char*)&a + 0), (char*)&a + 1, *((char*)&a + 1), (char*)&a + 2, *((char*)&a + 2), (char*)&a + 3, *((char*)&a + 3)); } bool is_little_endian(void) { int a = 0x1; return *(int *)&a == a; } int main(int argc, char *argv[]) { printf("%d\n", is_little_endian()); puts("---------------------"); test(); return 0; }