c language-unions and enumerations


1. Consortium

(1) Like a structure, a union is also composed of one or more members, and these members can be of different types.
(2) But the compiler only allocates enough memory space for the largest member. The characteristic of a union is that all members share the same memory space. Therefore, the union is also called: commonwealth.
(3) Assign a value to one member of the union, and the values ​​of other members will also change accordingly.

1. Declaration and creation of union types

关键字:union
Statement:

#include <stdio.h>
//联合类型的声明
union Un// 类型名
{
    
    
	char c;//成员
	int i;
};
int main()
{
    
    
	union Un s = {
    
    0};//创建
	return 0;
}

2. Characteristics of the consortium

The members of a union share the same memory space, so the size of a union variable must be at least the size of the largest member (because the union must be able to store at least the largest member).

//代码1
#include <stdio.h>
//联合类型的声明
union Un
{
    
    
 char c;
 int i;
};
int main()
{
    
    
 //联合变量的定义
 union Un un = {
    
    0};
 // 下⾯输出的结果是⼀样的吗?
 printf("%p\n", &(un.i));
 printf("%p\n", &(un.c));
 printf("%p\n", &un);
 return 0;
 }

operation result:

Insert image description here
Their addresses are the same, indicating that they share a space
Let’s give another example

union Un
{
    
    
	char c;
	int i;
};
int main()
{
    
    
	//联合变量的定义
	union Un un = {
    
     0 };
	un.i = 0x11223344;
	printf("%x\n", un.i);
	un.c = 0x55;
	printf("%x\n", un.i);
	return 0;
}

Run result:
Insert image description here
Why are the results different?
is because they share a space, and when c is used, the value of i will be changed.
Memory layout:
Insert image description here

3. Calculation of consortium size

The size of the union is at least the size of the largest member.
When the maximum member size is not an integer multiple of the maximum alignment number, it must be aligned to an integer multiple of the maximum alignment number.

like:

#include <stdio.h>
union Un1
{
    
    
 char c[5];
 int i;
};
union Un2
{
    
    
 short c[7];
 int i;
};
int main()
{
    
    
 //下⾯输出的结果是什么?
 printf("%d\n", sizeof(union Un1));
 printf("%d\n", sizeof(union Un2));
 return 0;
}

Run result:
Insert image description here
When encountering an array, we only look at the type, such as char c[5];-> We only look at the alignment number of char, and the alignment number of char is 1
The first output result
The maximum number of alignments of this type is 4 (int)
The size of the array is 7, we To ensure its size, it can only be a multiple of the maximum alignment number, so it is 8
The second output result is the same

4. Summary

(1) Using a union can save space
(2) When using one member of the union, do not use other members (because the values ​​of other members will change)

2. Enumeration

1. Declaration of enumeration type

Keyword: enum
As the name suggests, enumeration is a list of items.
List the possible values ​​one by one.

For example, in our real life: Monday to Sunday are a limited number of 7 days in a week, which can be listed one by one
Gender includes: male, female, confidential, or one by one There are 12 months in the list. You can also enumerate the three primary colors one by one. You can also use enumeration to express the meaning of these data.

like:

enum Day//星期
{
    
    
 Mon,
 Tues,
 Wed,
 Thur,
 Fri,
 Sat,
 Sun
};
enum Sex//性别
{
    
    
 MALE,

 FEMALE,
 SECRET
}enum Color//颜⾊
{
    
    
 RED,
 GREEN,
 BLUE
};

The enum Day , enum Sex , and enum Color defined above are all enumeration types.
The contents in {} are possible values ​​of the enumeration type, also called enumeration constants.
These possible values ​​are all valid, starting from 0 by default and increasing by 1 in sequence. Of course, an initial value can also be assigned when declaring an enumeration type.
For example:

enum Color//颜⾊
{
    
    
 RED=2,
 GREEN=4,
 BLUE=8
};

2. Advantages of enumeration types

Why use enumerations? We can use #define to define constants, why do we have to use enumerations? Advantages of enumerations:

  1. Increase code readability and maintainability
  2. Compared with identifiers defined by #define, enumerations have type checking, which is more rigorous.
  3. To facilitate debugging, symbols defined by #define will be deleted during the preprocessing phase.
  4. Easy to use, multiple constants can be defined at one time
  5. Enumeration constants follow the scope rules. The enumeration is declared within a function and can only be used within the function.

3. Use of enumeration types

enum Color//颜⾊
{
    
    
 RED=1,
 GREEN=2,
 BLUE=4
};
enum Color clr = GREEN;//使⽤枚举常量给枚举变量赋值

Is it possible to assign integers to enumeration variables? This is possible in C language, but not in C++. C++ type checking is stricter.

The above is what I shared. If there are any mistakes, please leave a message in the comment area.
Finally, thank you everyone for watching!

Guess you like

Origin blog.csdn.net/2302_79539362/article/details/134764302