C语言位段问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 02:27:37
void main()
{
struct A
{
unsigned one: 2;
unsigned two: 1;
unsigned three: 1;
}a;

printf("%d\n", sizeof(a));
}

为什么输出是4呢?
还有麻烦说明下位段这样的结构体是怎么分配空间的?

这里是字节对齐问题,可是个大问题
这里简单的说就是以2个字节对齐,
struct A
{
unsigned one: 2;
unsigned two: 1;
unsigned three: 1;
unsigned three: 1;
}a

信不信sizeof是6??
到网上搜点字节对齐的文章看看,这里知识还不少,考试(很多类型的考试,包括公司面试)的时候也爱考
自己参考:(如有侵权,非我所愿!)

http://blog.chinaunix.net/u1/49467/showart_424793.html

分配4的整数倍字节.