概念
大端(存储)模式:是指一个数据的低位字节的内容存放在高地址处,高位字节的内容存放在低地址处。
小端(存储)模式:是指一个数据的低位字节的内容存放在低地址处,高位字节的内容存放在高地址处。
代码
#include<stdio.h>
typedef union{
int value;
char buf[4];
}Test;
int
main(int argc, char *argv[])
{
Test test_value;
char i = 0;
test_value.value = 0x11223344;
for (i = 0; i < 4; i++)
printf("%x ",test_value.buf[i]);
return 0;
}
/**平台:Windows7 + CodeBlocks(小端模式)
* 程序输出结果:
***************************
* 44 33 22 11
***************************
*/
分析
联合变量test_value在内存中的位置是:
内存地址(假设地址是从1开始的) | 1 | 2 | 3 | 4 |
int | 44 | 33 | 22 | 11 |
char | buf[0] | buf[1] | buf[2] | buf[3] |