Understanding and application of pointer reference

1. Pointer

The pointer variable can be used to save the address (usually understood as an address)

When we read a pointer, we have to know two things

1) Pointer type

From a syntactic point of view, you just remove the pointer name from the pointer declaration statement, and the rest is the type of the pointer. This is the type of the pointer itself. Let's take a look at the types of each pointer in example 1: 
(1)int*ptr;//The type of pointer is int* 
(2)char*ptr;//The type of pointer is char* 
(3)int**ptr;//The type of pointer is int** 
(4)int(*ptr)[3];//The type of pointer is int(*)[3] 
(5)int*(*ptr)[4];//The type of pointer is int*(*)[4]

2) The type pointed to by the pointer (the type pointed to by the pointer represents the type of data stored in the address of the pointer)

When you access the memory area pointed to by the pointer, the type pointed to by the pointer determines what the compiler will treat the contents of that memory area. 
Grammatically, you only need to match the pointer name in the pointer declaration statement with the pointer declarator to the left of the name*Remove, and the rest is the type pointed to by the pointer. For example: 
(1)int*ptr; //The type pointed to by the pointer is int 
(2)char*ptr; //The type pointed to by the pointer is char 
(3)int**ptr; //The type pointed to by the pointer is int* 
(4)int(*ptr)[3]; //The type pointed to by the pointer is int()[3] 
(5)int*(*ptr)[4]; //The type pointed to by the pointer is int*()[4] 
In the arithmetic operation of pointer, the type pointed to by pointer plays a great role. 
Type of pointer(That is, the type of the pointer itself)And the type pointed to by the pointer are two concepts.

Other knowledge points

1. The value of the pointer or the memory area pointed to by the pointer

The value of the pointer is the value stored in the pointer itself. This value will be regarded by the compiler as an address rather than a general value. In 32-bit programs, the value of all types of pointers is a 32-bit integer, because the memory addresses in 32-bit programs are all 32 bits long. The memory area pointed to by the pointer is a memory area with a length of Si zeof (the type pointed to by the pointer), starting from the memory address represented by the value of the pointer. Later, when we say that the value of a pointer is XX, it is equivalent to saying that the pointer points to a memory area with XX as the first address; When we say that a pointer points to a memory area, we mean that the value of the pointer is the first address of the memory area. The memory area pointed to by the pointer and the type pointed to by the pointer are two completely different concepts. In example 1, the type pointed to by the pointer already exists, but the memory area pointed to by the pointer does not exist or is meaningless because the pointer has not been initialized.  
In the future, whenever you encounter a pointer, you should ask: what is the type of this pointer? What type does the pointer refer to? Where does the pointer point? (key points)  

2. The memory area occupied by the pointer itself

How much memory does the pointer itself occupy? You just need to test it with the function sizeof (pointer type). On 32-bit platforms, the pointer itself occupies a length of 4 bytes. The concept of memory occupied by the pointer itself is very useful in judging whether a pointer expression (which will be explained later) is an lvalue.

3. Special pointer

this   Pointer

this pointer concept
As we know earlier, in C + +, member variables and member functions are stored separately
Each non static member function will only produce a function instance, that is, multiple objects of the same type will share a piece of code
So the question is: how does this piece of code distinguish that object from calling itself
C + + solves the above problem by providing a special object pointer, this pointer, which points to the object to which the called member function belongs

this pointer is a pointer implied in each non static member function
this pointer does not need to be defined and can be used directly

Purpose of this pointer:
one      When a formal parameter has the same name as a member variable, it can be distinguished by the this pointer
two      Return the object itself in the non static member function of the class, and use return*this

Null pointer

Null pointer access member function
C + + hollow pointer can also call member functions, but pay attention to whether this pointer is used
If this pointer is used, it needs to be judged to ensure the robustness of the code

*Relationship with &

p Represents the address to
*p It refers to the content in the address
 here&Is the address operator,*Is an indirect operator. 
&a The result of the operation is a pointer of type a Type plus*,The type pointed to by the pointer is a The type of, the address pointed to by the pointer, that is a Your address. 
*p The results of the calculation are diverse. in short*p The result is p It points to something that has these characteristics: its type is p The type pointed to, and the address it occupies is p The address pointed to. 
Example 6: 
int a=12; int b; int *p; int **ptr;
p=&a; //&The result of a is a pointer, the type is int *, and the type pointed to is
//int, the address pointed to is the address of a.
*p=24; //*The result of p, where its type is int and its address is
//The address pointed to by p, obviously, * p is the variable a.
ptr=&p; //&The result of p is a pointer whose type is the type of p Plus a *,
//Here is int * *. The type pointed to by the pointer is the type of p, which
//Inside is int *. The address pointed to by the pointer is the address of the pointer p itself.
*ptr=&b; //*ptr is a pointer, the result of & B is also a pointer, and these two pointers
//The type of is the same as the type pointed to, so use & B to assign * ptr
//Value is no problem.
**ptr=34; //*The result of ptr is what ptr points to, which is a pointer here,
//Do another * operation on this pointer, and the result is a variable of type int.

in general

int *p ; 

1. * role dereference

2. p is an address, so when initializing (that is, generally speaking)   int *p =&a;   = And the following part (this process)

=It must be followed by an address (supplement: the array name is the address of the first element of the array. For the address of the first declared member of the class, it is the address of the class. Generally, the address can be obtained by using the & address and the operator)

Supplement to the knowledge in brackets 1. * (arr+n)   Equivalent to arr[n]  

3.int * is the pointer type --- usually, the pointer type is the combination of * and the type on the left

4 int is the type pointed to by the pointer --- usually the type on the left of * is the type pointed to by the pointer

The following is the understanding of a single pulled pointer (basic)

int p; //This is an ordinary integer variable
1.int *p; //First, start at P and combine it with * first, so it means that P is a pointer, and then combine it with int to indicate that the type of content pointed to by the pointer is int. therefore, P is a pointer that returns integer data
2.int p[3]; //First, start at P, first combine with [], indicating that P is an array, and then combine with int, indicating that the elements in the array are integer, so p is an array composed of integer data
3.int *p[3]; //First, start from P and combine with []. Because its priority is higher than *, P is an array, and then combined with *, indicating that the elements in the array are pointer types, and then combined with int, indicating that the type of content pointed to by the pointer is integer, so p is an array composed of pointers that return integer data
4.int (*p)[3]; //First, start at P and combine it with *, indicating that P is a pointer, and then combined with [] (the step of "()" can be ignored, just to change the priority), indicating that the content pointed to by the pointer is an array, and then combined with int, indicating that the elements in the array are integer. Therefore, P is a pointer to an array composed of integer data
5.int **p; //First, start with P, first combine it with * to say that P is a pointer, and then combine it with * to indicate that the element pointed to by the pointer is a pointer, and then combine it with int to indicate that the element pointed to by the pointer is integer data. Since secondary pointers and higher-level pointers are rarely used in complex types, we will not consider multi-level pointers for later more complex types, Only one level pointer is considered at most
6.int p(int); //Starting from P, first combine it with (), indicating that P is a function, and then enter () for analysis, indicating that the function has an integer variable parameter, and then combine it with the external int, indicating that the return value of the function is an integer data
7.int (*p)(int); //Starting from P, first combine it with the pointer, indicating that P is a pointer, then with (), indicating that the pointer points to a function, and then combined with int in (), indicating that the function has an int parameter, and then combined with the outermost int, indicating that the return type of the function is an integer, so p is a pointer to a function with an integer parameter and the return type is an integer
8.int *(*p(int))[3]; //You can skip it first, regardless of this type. It is too complex. Start with P, combine it with (), indicating that P is a function, and then enter () and combine it with int, indicating that the function has an integer variable parameter, and then combine it with the external * to indicate that the function returns a pointer, and then go to the outermost layer, combine it with [] to indicate that the returned pointer points to an array, and then combine it with *, Note that the elements in the array are pointers, and then combined with int to indicate that the content pointed to by the pointer is integer data. Therefore, P is a function whose parameter is an integer data and returns a pointer variable pointing to the array composed of integer pointer variables

2. Most important   Shangxian Fuhua Shenzhou tablet, with an inch of strength, is the first in the world -- a Fuhua single pusher

3. Reference

A reference can be simply understood as taking an alias  

int &a=b;

b over there is the alias of a

Continue int & B = C;

View abc   The result is the same

4. References and pointers

References and pointers are used in functions and classes  

To understand why they are used, we need to know what they can do. The popular point of quotation and pointer can be understood as a means of transportation to a designated place

Now let's supplement the features of function calls

When calling a function, what I fear most is that the operation is as fierce as a tiger, and then change the function beyond recognition. The crying of the function indicates your uncle's, so in order to prevent our misoperation   So when the compiler calls a function   Instead of as like as two peas, you use the same clone as the clone, and use the clone to do the corresponding operation.   Then the return value is sent to the register, and then the function we call directly receives the things in the register,   Of course, we not only clone the function, but also prepare the clone for the data we call, so the data we modify is the clone data. The clone is full of scars and crying, and we watch the play next to the ontology.   But we just want to change the ontology. What should we do? At this time, the strongest tool pointer of C + + and his younger brother's reference come on stage  

Pointers and references go into the target data address and get the target data tool. The tool people of function cloning are very tool people and will not change their functions. Therefore, they can still go in and operate on the original. Therefore, the purpose of pointer references in general functions and classes is more or less. In order to modify the original, it is generally modified, because it is unnecessary to display anything.


 

Keywords: C++ pointer

Added by Simon Mayer on Tue, 28 Sep 2021 13:14:02 +0300