#define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
同时大小端测试 如下
#include <stdio.h>
#include <stdlib.h>
#define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
int main(void)
{
unsigned int u32RawData;
unsigned char *pu8CheckData;
u32RawData = 0x11223344; //Assign data
printf("%ld\n", my_sizeof(u32RawData));
printf("%p\n", (&u32RawData+1 - &u32RawData));
printf("%p\n", (char*)(&u32RawData+1)-(char*)(&u32RawData));
printf("%p\n", (char*)(&u32RawData));
pu8CheckData = (unsigned char *)&u32RawData; //Type cast
if (*pu8CheckData == 0x44) //check the value of lower address
{
printf("little-endian");
}
else if (*pu8CheckData == 0x11) //check the value of lower address
{
printf("big-endian");
}
return 0;
}
在x86平台测试是小端模式