(8) Multi-level pointer

First look at the express cabinet in life:Insert picture description here
  the grid here is the memory unit, the number is the address, and the things in the grid correspond to the content stored in the memory.
  Suppose I put a book in grid 03, and then tell you the number 03, you can get the book inside according to 03. So if I put the book in box 05, and then put only a small note in box 03, it says: "Book in box 05".
  what will you do? Of course, I opened the 03 grid, then took out the note, and opened the 05 grid according to the above content to get the book. The number 03 grid here is called a pointer, because it contains a small note (address) pointing to other grids instead of a specific book. do you understand? Then if I put the book in box 07, and then put a note in box 05: "Book on number 07", and put a note in box 03 "Book on 05"

Insert picture description here
  Here, the grid No. 03 is called the secondary pointer, the grid No. 05 is called the pointer, and the number 07 is our usual variable. In turn, N-level pointers can be introduced.
  So do you understand? The same piece of memory, if it stores the address of another variable, it is called a pointer , and it stores the actual content, it is called a variable .

int a;
int *pa = &a;
int **ppa = &pa;
int ***pppa = &ppa;

In the above code, pa is called the first-level pointer, which is usually referred to as the pointer, and ppa is the second-level pointer.

The memory diagram is as follows:
Insert picture description here
 No matter how many levels of pointers, there are two core things:

  • The pointer itself is also a variable and needs to be stored in memory, and the pointer also has its own address

  • The pointer memory stores the address of the variable it points to

  How to interpret the expression of int a? int ** a can be divided into two parts, namely int and a. The back a indicates that a is a pointer variable, and the front int indicates that the pointer variable a can only store the address of an int variable. For two-level pointers or even multi-level pointers, we can split it into two parts. First of all, no matter how many levels of the pointer variable, it is a pointer variable first, the pointer variable is a *, and the rest represents the address of what type of variable this pointer variable can only store. For example, int a means that the pointer variable a can only store the address of an int variable.


An example of a test is as follows:

int main()
{
    
    
	int a =10;
	int *p1 = &a;
	int **p2=&p1;
	int ***p3=&p2;
	cout << "========================" << endl;
	cout << "&a   -----" << &a<<endl;

	cout << "========================" << endl;
	cout << "*p1  -----" << *p1 << endl;
	cout << "p1   -----" << p1 << endl;         // 和a地址相同

	cout << "========================" << endl;
	cout << "**p2 -----" << **p2 << endl;
	cout << "*p2  -----" << *p2<< endl;         // 和p1地址相同
	cout << "p2   -----" << p2 << endl;

	cout << "========================" << endl;
	cout << "***p3-----" << ***p3 << endl;
	cout << "**p3 -----" << **p3 << endl;       // 和*p2、p1地址相同
	cout << "*p3  -----" << *p3 << endl;        // 和p2地址相同
	cout << "p3   -----" << p3 << endl;
}

Output result:

========================
&a   -----0055FACC
========================
*p1  -----10
p1   -----0055FACC
========================
**p2 -----10
*p2  -----0055FACC
p2   -----0055FAC0
========================
***p3-----10
**p3 -----0055FACC
*p3  -----0055FAC0
p3   -----0055FAB4

Guess you like

Origin blog.csdn.net/qq_40329851/article/details/114861129