共用体判断:
/**********************
*判断机器是大端还是小端
*Author:shi
*
* ********************/
#include <stdio.h>
union test{
int a;
char b[4];
};
//系统为共用体test分配4字节空间,a和b共用这段储存空间
int main(void)
{
union test p;
p.a = 1;
if(p.b[0] == 1)
printf("小端\n");
else if(p.b[3] == 1)
printf("大端\n");
//a = 1;即00 01;01是低地址
//若1保存在b[0],则为低字节保存在低地址,即小端机器
return 0;
}
地址判断:
int main(void)
{
int x = 0x1122;//0x11为高地址,0x22为低地址
char *y = NULL;
y = (char *)(&x);//类型强转
if(y[0] == 0x22)
printf("小端\n");
else
printf("大端\n");
return 0;
}