C语言位段与联合体(union)的内存分配

时间:2022-09-05 17:03:53
 union V {
struct X {
unsigned char s1:1;
unsigned char s2:1;
unsigned char s3:4;
} x;
unsigned char s1:1;
unsigned char s2:1;
unsigned char s3:4;
int c;
} v;
void main()
{

v.c = 100; //0110 0100
printf("%d", v.x.s3); //9 内存从左到右依次分配
printf("%d", v.s3); //4 内存都从最右边同一位置开始分配(共用)


system("pause");
}