- 论坛徽章:
- 0
|
这个通常编译器的缺省值有关, 如果需要跨平台, 通常要在源码(通常是头文件)内用#pragma指定适当的对其大小:
[code]
/*
* packsize.c
* by mymtom. 2008-02-15
*/
#include <stdio.h>
struct foo {
char a;
int b;
short c;
};
#pragma pack(push, 2)
struct bar {
char a;
int b;
short c;
};
#pragma pack(pop)
struct mym {
char a;
int b;
short c;
};
#pragma pack(push, 1)
struct tom {
char a;
int b;
short c;
};
#pragma pack(pop)
int main(void)
{
(void)printf("sizeof struct foo = %zd\n", sizeof(struct foo));
(void)printf("sizeof struct bar = %zd\n", sizeof(struct bar));
(void)printf("sizeof struct mym = %zd\n", sizeof(struct mym));
(void)printf("sizeof struct tom = %zd\n", sizeof(struct tom));
return 0;
}
[/code] |
|