//Common Body Union#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<string.h>//Union common body, construction data type, also called Union, Purpose: More than 10 variables of different types share a memory (mutual coverage) //The shared body does not allocate memory when the type is defined, but allocates memory when defining a common body variableUnion data{CharC; inta[Ten];} Udat1;union stu{intA//internal variable cannot be initialized because the type definition does not allocate memory Charname[Ten];} FISR,*FISR1, fisr2[Ten];//common body definition form ①//Note: Initialization can only initialize the first member variable when defining a common body variable, and cannot initialize other member variables//Anonymous public body-Anonymous common body cannot define variables, only define variables at initialization time//This limits the number of shared-body variables, which are commonly used for the use of variables for certain special permissionsUnion {CharC; inta[Ten];} LASTP;structtes{intnum; Charname[ +];} Tes1= {4,"Feiyu"},tes2;//The number of memory units that a common body variable occupies should be greater than or equal to the number of memory units that its internal maximum data member occupies. //struct, the common body is stored in memory, byte-aligned//data is a common body, and the common body size must contain at least the largest member data, so its original size is sizeof (int) *10 = 40 bytes,//Union {//char c;//int a[10];//};//because data is a multiple of its original size, which is exactly the widest base type member int, no padding bytes//so the size of the common body data is//Stu is a common body, the original size of Stu is sizeof (char) * 10 bytes, according to the principle of byte alignment, the widest base type member is int, need to fill byte//so the size of the common body Stu is//Summary: The common body size must contain at least the largest member data and can be divisible into the widest base type member-details see struct voidMain () {//Verifying the byte alignment principle of a common bodyprintf"to verify the byte alignment principle of a common body--%d\n",sizeof(UDAT1)); printf ("to verify the byte alignment principle of a common body--%d\n",sizeof(FISR)); //define a common bodyUnion Stu Stuinfo;//common body definition form ②//assignment of a common bodySTUINFO.A = +; //shared body variable at any time, only one member exists, and when a member is present, it is assigned to the other member, which is overwritten by the previous//because a common body is a total memory, it does not allocate a chunk of memory to each member variable.strcpy (Stuinfo.name,"Lenovo"); printf ("shared body member variable a=%d;name=%s\n", STUINFO.A, stuinfo.name); //The result is that member variable A is overwrittenUnion stu Stuinfo2 = Stuinfo;//A shared body variable can be directly assigned to a valueprintf"shared body member variable a=%d;name=%s\n", stuinfo2.a, stuinfo2.name); Tes2= Tes1;//struct variable can be directly assigned valueprintf"struct member variable num=%d,name=%s\n", Tes2.num, tes2.name); System ("Pause");}
C Language Common body