Master advanced structures (C language) from shallow to deep

Foreword
In this issue we will continue to explain the knowledge of structures. Friends who have not read the previous issue must hurry up and learn it.
Last issue, rush the duck!
So without further ado, let’s start today’s study!
Insert image description here

1. Self-reference of structure

What is the self-reference of a structure? To put it simply, you just pretend to be yourself (somewhat similar to recursion).
So is it self-referential like this?

struct student
{
    
    
char name[20];
int age;
float score;
struct student next_student;//这是我们自己定义的一个类型的解耦提变量如果可以这样写,我们就可以在一个学生的结构体里包含下一个学生的结构体,如果有一百个学生,我们就可以通过第一个学生访问到第一百个学生
}stu1;

But there seems to be a problem with writing this way. We have just learned about memory alignment. During compilation, the compiler needs to know the size of the structure variable to allocate space for the structure variable, but it seems that there is no way to know the size of the structure variable. , will fall into infinite recursion. So can we change our thinking? Instead of adding the next student's structure variable to the structure member, wouldn't it be enough to add the address of the next student's structure variable, so that we won't fall into infinite recursion. And we can find the next student's information through the address. It is written as follows:

struct student
{
    
    
char name[20];
int age;
float score;
struct student* next_student;//储存下一个学生(结构体变量)的地址
}stu1;

2. Anonymous structure

What is an anonymous structure? Take the structure of type student we just created. What if we write code like this?

struct 
{
    
    
char name[20];
int age;
float score;
}stu1;

Did you find that student is missing? This special declaration is called an anonymous structure.
However, it should be noted that variables of anonymous structures can only be created when declared and can only have one variable. In other words, it can only be used once, and we cannot self-reference the anonymous structure.

3, bit segment

Before learning about bit segments, let’s look at the following code.

struct student
{
    
    
char name[20];
int age:6;
int score:8;
}

We found that there is an extra: and a number after the age and fraction in the members of this structure. What does this mean? Under normal circumstances, a student's age should be between 6 and 31 years old. Because the age is an integer data, 4 bytes of space will be allocated to it. However, if you only want to express the numbers 0-31, there is no need at all. For a large space, only 6 bits are enough for signed integer. So back to the code, int age: 6; means to allocate 6 bits of space to this integer. The score allocates 8 bytes of space to him. So how is the memory of the structure allocated at this time?

struct uma
{
    
    
int a:2;
int b:3;
int c:5;
int d:10;

Take this structure as an example.
Insert image description here
Compared with this picture, you can understand how the bit segments allocate space.
But we also need to pay attention to the following points.
1. The members of the bit field must be int, unsigned int or signed int. In C99, the type of the bit field members can also be selected from other types.
2. Bit segments involve many uncertain factors. Bit segments are not cross-platform. Programs that focus on portability should avoid using bit segments.

4. Passing parameters of structure

How do we pass parameters to the structure in the function?
We have the following two methods

#include <stdio.h>
struct Student
{
    
    
	int data[1000];
	int num;
};
struct Student s = {
    
     {
    
    1,2,3,4}, 1000 };
//结构体传参
void print1(struct Student s)
{
    
    
	printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct Student* ps)
{
    
    
	printf("%d\n", ps->num);
}
int main()
{
    
    
	print1(s); //传结构体
	print2(&s); //传结构体的地址
	return 0;
}

So which of these two ways of writing is better? That's right, it's the second one, because if you pass the structure directly, the computer will allocate a new memory space for the formal parameters. It's okay to say small, but if it is a particularly large structure, it will waste a lot of memory, but passing the address is different. There is no need to re-open a space, which can improve the performance of the program.

5, epilogue

Friends who have studied these two blogs must have a very good understanding of the use of structures, but they also need to practice more. As the saying goes, practice makes perfect. Next time I will find a few questions about structures to share with you. . Structure exercise link, gogogo!
If you think the blogger’s words are good, don’t forget to follow the blogger. Your attention is my biggest motivation!

Guess you like

Origin blog.csdn.net/Tokai___Teio/article/details/134796957