#include<stdio.h>
union Student{
int i;
unsigned char ch[9];
};
int main (){
Student student;
student.i=0x1632;
printf("%x\n",student.ch[0]); //32
printf("%x\n",student.ch[1]); //16
printf("%p\n%p\n",&student.i,&student.ch);
printf("%d\n",sizeof(student)); //12
printf("%d\n",sizeof(student.i)); //4
printf("%d\n",sizeof(student.ch)); //9
return 0;
}
联合体union的内存取内部变量中大者。各变量共用一块内存,对其中一个变量赋值,即是对所有的变量同时赋值。