自己实现sizeof+大小端测试

2020-09-07 11:38:47 浏览数 (1)

代码语言:javascript复制
#define my_sizeof(type) ((char *)(&type 1)-(char*)(&type))

同时大小端测试 如下

代码语言:javascript复制
#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("%ldn", my_sizeof(u32RawData)); 
    printf("%pn", (&u32RawData 1 - &u32RawData)); 
    printf("%pn", (char*)(&u32RawData 1)-(char*)(&u32RawData)); 
    printf("%pn", (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平台测试是小端模式

0 人点赞