enumerate? joint? got it

    "Qingming" Du Mu During the
Qingming season, it rained heavily, and pedestrians on the road wanted to break their souls.
Where is the restaurant? The shepherd boy pointed to Xinghua Village.


foreword

Now that we've learned the basics of structures, let's learn about
other forms of data.


1. Concept

1. Enumeration

  Enumeration, as the name suggests, is an enumeration.
  List the possible values.
  For example in our real life:

 From Monday to Sunday, you can list them one by one.
 Twelve months a year, you can list them one by one.

  If we create an enumeration variable Day, no assignment is made.

enum Day
{
    
    
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday
};

insert image description here
  If we assign a value to Wednesday in the enum, that is, Wednesday = 5;
then Thursday becomes a number 6 after 5, and the same is true for Friday and Saturday.
insert image description here

2. United

  Union, also known as union .
  A union is a special custom type that contains a series of member variables, characterized by the fact that these members share a space .
  How is sharing manifested?
  We execute the following code:

union Un
{
    
    
	char c;//1
	int i;//4
};

int main()
{
    
    
	union Un u;
	printf("%p\n", &u);//打印联合体地址
	printf("%p\n", &(u.c));//打印c的地址
	printf("%p\n", &(u.i));//打印i的地址
}

  We found that the addresses of the members were the same.
  This shows that the union does have the characteristics of " sharing ".
  We use images to further understand.
insert image description here
  At this time, careful friends found it. This will not work! If you change one of them, doesn't the other suffer as well?
  But don't panic, because at the same time, we only use one of them, so even if we change it, it will not have any effect.
  For example, when we describe a person's occupation, at the same time , we only say that he is a teacher, but we do not say that he is a teacher, and that he is a student.

Tips :
The members of the union share a piece of memory space . The minimum size of such a union variable is the size of the largest member.

2. Why use enumeration and union

1. The advantages of enumeration

1. Increase the readability and maintainability of the code
  
2. Compared with the identifier defined by #define, it has type checking, which is convenient for debugging
  
3. Prevents naming pollution
  
4. It is easy to use, and multiple constants can be defined at one time

2. The advantages of joint

Unions take up less memory space than structs.

3. Enumeration and joint use of examples

1. Precautions for enumeration variables

enum Day		
{
    
    
	Monday=1,
	Tuesday,
	Wednesday,
};

enum Day kite = Monday;

You can only assign an enumeration constant to an enumeration variable, and there will be no type difference.
It is legal to assign a value to kite as above, but
it is illegal to write kite = 1 directly.

2. Jointly realize the judgment of size end

Using the union, we can write the following program to determine the memory mode of the compiler

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int check_cls()
{
    
    
	union Un
	{
    
    
		char c;
		int i;
	}u;
	u.i = 1;		//给i赋值1,此时c也被改变
	return u.c;		//若为小端,c为01
					//若为大端,c为00
}

int main()
{
    
    
	if (check_cls() == 1)
	{
    
    
		printf("小端\n");
	}
	else
	{
    
    
		printf("大端\n");
	}
}

3. Calculation of joint size

  As mentioned above, the size of the union is at least the size of the largest member.
  So what is the size of this largest member?
  Let's look at the following program:

union Un
{
    
    	
	char arr[5];//5
	int i;//4
}u;

int main()
{
    
    
	printf("%d", sizeof(u));
}

At first glance, five bytes are enough,
but the actual size of u is 8 bytes.
How can this happen QAQ
This reminds me of the good brother structure of the union ,
his good brother,
full of memory alignment ,
this union, too.
Let's look at its members, an array of char type, the alignment number depends on its element type char, the alignment number is 1; an integer variable of int type, the alignment number is 4, so the maximum alignment number is 4 .
In order to fit, the final size is aligned to 8.

Tips :
Union also has memory alignment .

Let's do the quiz to see if you have learned it! ! !

下面代码的结果是:( )
#include <stdio.h>
union Un
{
    
    
	short s[7];
	int n;
};
int main()
{
    
    
  printf("%d\n", sizeof(union Un));
  return 0;
}
//选项:
A .14
B .4
C .16
D .18

The answer is C!

Solution : The union is aligned to int, 7 shorts are 14 bytes in total, and 16 bytes after alignment. n is a separate 4 bytes. Because it is a union, n and s share space, and only the longest element is taken, so it occupies 16 bytes.

Four. Several classic examples

1. Enumeration

The result of the following code is: ( )

enum ENUM_A
{
    
    
		X1,
		Y1,
		Z1 = 255,
		A1,
		B1,
};
enum ENUM_A enumA = Y1;
enum ENUM_A enumB = B1;
printf("%d %d\n", enumA, enumB);
//选项:
A .1, 4
B .1, 257
C .2, 257
D .2, 5

The answer to this question is B,
notice that Z1 is assigned 255,
X1, Y1, Z1, A1, B1 are 0, 1, 255, 256, 257 in turn
insert image description here


2. United

The output of the following program is: ()

#include<stdio.h>
int main()
{
    
    
  union
  {
    
    
    short k;
    char i[2];
  }*s, a;
  s = &a;
  s->i[0] = 0x39;
  s->i[1] = 0x38;
  printf(%x\n”,a.k);
  return 0;
}
//选项:
A .3839
B .3938
C .380039
D .不确定


The answer is A. Answer : As shown in the figure, the bit order is similar to little endian, and the low address is in the low position, so 39 is the low address, in the low position, and 38 is in the high position, so it is 3839, so choose A.
insert image description here

Summarize

  Today's preliminary explanation of enumeration and union is over!
  Hope you guys like it! ! ! OvO

Guess you like

Origin blog.csdn.net/m0_63742310/article/details/123948361
got
Recommended