文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[字节对齐][示例]

[字节对齐][示例]

时间:2010-12-28  来源:kevin33643

打印结果为:12 12 16  

#include <stdio.h>

#pragma pack(4)/*编译选项,表示4字节对齐*/

int main(int argc, char* argv[])
{
    struct tagTest1
    {
        short a;
        char d;
        long b;
        long c;
    };
    struct tagTest2
    {
        long b;
        short c;
        char d;
        long a;
    };
    struct tagTest3
    {
        short c;
        long b;
        char d;
        long a;
    };
    struct tagTest1 stT1;
    struct tagTest2 stT2;
    struct tagTest3 stT3;
    
    printf("%d %d %d\n", sizeof(stT1), sizeof(stT2), sizeof(stT3));
    return 0;
}
#pragma pack()


打印结果为:01 27 00 00  

#include <stdio.h>

#pragma pack(4)

int main(int argc, char* argv[])
{
    unsigned char puc[4];
    struct tagPIM
    {
        unsigned char ucPim1;
        unsigned char ucData0:1;
        unsigned char ucData1:2;
        unsigned char ucData2:3;
    }*pstPimData;

    pstPimData = (struct tagPIM *)puc;

    memset(puc, 0, 4);
    pstPimData->ucPim1 = 1;
    pstPimData->ucData0 = 1;
    pstPimData->ucData1 = 3;
    pstPimData->ucData2 = 4;

    printf("%02X %02X %02X %02X\n", puc[0], puc[1], puc[2], puc[3]);
    
    /*
    int i;
    for(i = 0; i < 8; ++i)
    {
        printf("%d\n", puc[1] & (0x1 << i));
    }
    */
    
    return 0;
}
#pragma pack()


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载