[字节对齐][示例]
打印结果为: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()
|
作者: kevin33643
发布时间: 2010-12-28