当前位置:网站首页>C language linked list points to the next structure pointer, structure and its many small details

C language linked list points to the next structure pointer, structure and its many small details

2022-06-24 03:56:00 Good pie notes

There are quite a few students studying C In the process of language , When you learn the linked list, you can't go around , It's too late to get to the point .

This paper attempts to interpret the linked list creation algorithm from scratch from the perspective of Xiaobai .

Before formally studying the linked list , Let's start with the structure . It's like a pointer , A structure is also a data type , But this data type can store complex data of multiple attributes , You need to define the type before using .

Its definition form is very simple :struct name { List of members ;}; For example, for a student , He can be named 、 Gender 、 Student number 、 Grades and so on , And these data can be in strings respectively 、 character 、 integer array 、float Type of data storage .

So we can define a student's structure , It contains all his attributes , Just note that we only define a data type , If you want to apply for a storage unit from memory, you should continue to declare variables .

Then let's talk about its use , Variables can be used , The premise is that you can express it . For simple data types ( such as int), We can peel the onion and add the structure name '.' The way to put it “ spot ” come out .

Here's another small detail , Is the processing of strings . String cannot be assigned , Only character arrays or character copy functions can be used strcpy() Etc . The name of the string represents the first address , Is the address constant , Constant cannot be assigned .

After defining the structure type, the system will not allocate units , Only when variables are defined will the system allocate units . Of course, you can also define structure arrays , The numbers in brackets indicate the length , The size of each unit is the length specified by the structure type .

We mentioned earlier that , If you want to operate on a certain section of memory, the premise is 【 Express the variable 】.

For structure pointers , You can look at the name and know the meaning : This is a pointer , Only the address stored in this pointer is the address of a structure variable .

For structure pointers , Members accessing the structure variable it points to can use the value taking operator *, such as struct (*stu).name. Of course , The way we prefer to use in practice is the arrow way :struct stu->name.

Let's look at something interesting :++p->age、(++p)->age、(p++)->age、p++->age. Actually C Similar expressions can be found everywhere in the language , Just keep your head clear when you see it , It is easy to pay attention to the operation priority and self increment processing , The reader can deduce .

stay C In language , If you want to use structural variables, you must mark them in front of you struct, To indicate that this is a structural variable ( Although in .cpp The file supports ellipsis ), This way of writing often makes the code verbose , So programmers often prefer to use alias keywords typedef.

You can use keywords at the beginning of the structure struct, In this way, the structure type or the corresponding pointer type can be aliased , In the process of use, there will be one less struct, Why not do it !

Just for beginners , It may be difficult to understand how to alias structure pointer types . Here we just need to treat it as an equivalent replacement , After aliasing the structure pointer, the pointer will be marked * Hide it , But in actual use, we should always pay attention to , This is still a pointer .

原网站

版权声明
本文为[Good pie notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210917190323822n.html