[C language] Custom types: unions and enumerations

Today I will share what I learned about unions and enumerations:

1. Declaration of union type

2. Characteristics of the consortium

3. Calculation of consortium

4. Declaration of enumeration types

5. Advantages of enumeration types

6. What are the uses of enumeration types?

1. Declaration of union type

The declaration of union and structure are very similar, except that struct is changed to union. Let’s take a look at it together:

union Un {
int a;
char b;
double c;
};

Of course, we can define the union variable while declaring it during the actual declaration, and it is also possible to initialize it by the way.

2. Characteristics of the consortium

The member settings in the union are similar to those of the structure, and all types of members are supported. However, the compiler will only open up enough space for the largest element in the memory, and other members share this set of space; when you want to modify a member When the value is changed, the values of other members will also change. (A federation is also called a community)

Note: The bit segments of the structure we talked about in the previous article do not need to consider the big and small endian, but here we have to consider the big and small endian because we need to look at the memory layout.

Please see the code:

Code 1:

#include<stdio.h>
union Un {
int a;
char b;
double c;
};
int main()
{
union Un s = { 0 };
printf("%p\\
", s);
printf("%p\\
", & amp;s);
printf("%p\\
", & amp;s.a);
printf("%p\\
", & amp;s.b);
printf("%p\\
", & amp;s.c);
return 0;
}

It shows that all members do share a set of spaces.

Code 2:

#include<stdio.h>
union Un {
int a;
char b;
double c;
};
int main()
{
union Un s = { 0 };
printf("%zd", sizeof(s));
return 0;
}

It shows that all members indeed share a set of spaces.

Let’s try changing the values:

Code 3:

#include<stdio.h>
union Un {
int a;
char b;
};
int main()
{
union Un s = { 0 };//Note: You cannot directly int a=0x11335577, char b=0x99, so you cannot directly modify the contents of the union.
//Otherwise, the printed a is still 0x11335577, and the members are directly accessed to achieve the effect of assignment.
s.a = 0x12345678;
s.b = 0x00000023;
printf("%x\\
",s.a);
return 0;
}

Here we draw a picture to explain:

Proved that they share a set of memory.

The size of the structure in the same situation is 8 bytes (int occupies 4 first, char can be anywhere, the memory size is an integer multiple of int and must be >= 4 + 1). This naturally leads to the size of the union structure we are going to talk about.

3. Calculation of consortium size

The calculation rule in my memory is:

1. The size of the entire union must be >= the element that occupies the largest memory in the union;

2. The size of the entire union is equal to an integer multiple of the maximum alignment number of all “bare elements”;

Now let’s write two questions to see if we can digest them:

#include <stdio.h>

union Un1

{
 char c[5];
 int i;
};

union Un2

{
 short c[7];
 int i;
};

int main()
{
 printf("%d\\
", sizeof(union Un1));
 printf("%d\\
", sizeof(union Un2));
 return 0;
}

I will explain it to you directly based on this code page:

#include <stdio.h>

union Un1

{
 char c[5];
 int i;
};//This answer should be 8. The largest memory occupancy is char[5] which occupies 5 bytes, and
//The alignment number of int after expansion is 4, the alignment number of a single char is 1, and the memory size
//It must be an integer multiple of 4, and the next 5 bytes of the package are required, so 8 is used as the memory size.

union Un2

{
 short c[7];
 int i;
};//The answer to this should be 16
//This array occupies the largest amount of memory, occupying 14 bytes.
//But the alignment number of a single short is 2, and the alignment number of int is 4.
//The memory is an integer multiple of 4, and it needs to be packed with 14, so
//The size of memory is 16

int main()
{
 //Next? What is the output result?
 printf("%d\\
", sizeof(union Un1));
 printf("%d\\
", sizeof(union Un2));
 return 0;
}

Let’s verify it:

Why should we learn about coalitions? At least we can save space by learning this (compared to structures of the same data type).

For example: when we create a menu, we have a set meal. The set meal includes the same rice, drinks, and soups, but the dishes and desserts are different. Some dishes also come with some side dishes. If we don’t learn In the case of unions, it is easy to just use nested structures directly, but in fact we might as well put a union outside and structures inside to reduce memory consumption.

Let’s use the knowledge of the union to determine whether the compiler is big-endian or little-endian:

#include<stdio.h>
union SN{
int a;
char b;
};
int check_sys(union SN n)
{
return(*(char*) & amp;(n.a));
}
int main()
{
union SN n = { 0 };//Initialization
n.a = 0;
n.b = 1;//Assign value to b
int ret = check_sys(n);
if (ret = 1)
{
printf("little endian\\
");
}
else
{
printf("Big endian\\
");
}
return 0;
}

We can also illustrate this by drawing a picture:

or

int check_sys(union struct SN)
{
s.a=1;//int type
return s.b;//char type (returns the content of the first byte)
}

4. Declaration of enumeration types

In reality, we have a lot of data to classify, such as weather, color, week, etc.

This is similar to the previous structure and union. Let’s go directly to the code:

#include<stdio.h>
enum Day {
MON,TWO,WED,THU,FRI,SAT,SUN
};
int main()
{
printf("%d %d %d %d %d %d %d\\
", MON, TWO, WED, THU, FRI, SAT, SUN);
return 0;
}

The enum Day here is an enumeration type, and the MON etc. inside are enumeration constants. Enumeration constants are usually assigned to enumeration variables.

The enumeration constants in the enumeration start from 0 and increase by 1 in sequence. If you don’t want it, just “assign” the value directly in the brackets.

5. Advantages of enumeration types

1. It can reduce the use of #define, and directly use an enumeration type to create multiple variables;

2. It can be used as a “local variable” defined variable and cannot be used in other places;

3. Can simplify the code and increase readability;

6. Use of enumeration types

Let’s look at the code:

It serves as a definition.

This ends today’s sharing, thank you all!