c language union and enumeration

joint

Joint definition

It is a special custom type. Variables defined by this type also contain a series of members. The characteristic is that these members share the same space (so the union is also called a union).
For example:
GREEN,
BLUE
};

//联合类型的声明
union Un 
{
    
     
 char c; 
 int i; 
}; 
//联合变量的定义
union Un un; 
//计算连个变量的大小
printf("%d\n", sizeof(un)); 

The characteristics of the joint

The characteristics of the
union The members of the union share the same memory space. The size of such a union variable is at least the size of the largest member (because the union must at least be able to save the largest member)

union Un
{
    
    
	int i;
	char c;
};
union Un un;
// 下面输出的结果是一样的吗?
int main()
{
    
    
	printf("%d\n", &(un.i));
	printf("%d\n", &(un.c));
	//下面输出的结果是什么?
	un.i = 0x11223344;
	un.c = 0x55;
	printf("%x\n", un.i);
	return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45849625/article/details/115030658