What is the function of the second-level pointer in C language
This article mainly introduces "what is the function of the second-level pointer of C language". In the daily operation, I believe that many people have doubts about the function of the second-level pointer of C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "what is the function of the second-level pointer of C language?" Next, please follow the editor to study!
I. concept
In the following pointing relationship between A to B and B to C:
First
C is "a piece of content". For example, if you allocate a piece of memory with malloc or new, and then insert "a piece of content", that is C. The starting address of C is 0x00000008.
B is a pointer variable, which stores the address of C, but B also takes up space, so B also has an address. The starting address of B is 0x00000004, but the address of C is stored in B memory, so the content of B is 0x00000008.
So it's easier to understand so far:
B = 0x00000008; / / the content of B
* B = "a piece of content"; / / B dereference, that is, the value of C pointed to by the B pointer
& B = 0x00000004; / / B takes the address, and the address of B is 0x00000004
So, let's take a look at A:
An is a secondary pointer variable, in which the address of B, 0x00000004, also has an address, which is 0x00000000
* A = B = 0x00000008; / / A dereference is the content of B.
* * A = * B = "a piece of content"; / / B dereference, that is, the value of C pointed to by the B pointer
A = & B = 0x00000004; / / A stores the address of B and the address of B is 0x00000004
& A = 0x0000000000; / A fetch address
The pointer to the pointer, that is, the secondary pointer, holds the address of the primary pointer, such as:
P is the first-level pointer, which stores the address of a; Q is the pointer to the pointer (second-level pointer), which stores the address of the first-level pointer (p); the content of Q is the value of 0xbfaca 770 ~ Q, that is, the content 0xbfaca776 that Q points to, that is, * Q is still an address, that is, the content of pointer p, that is, * qqqp.
2. Use
The function of the second-level pointer as a function parameter: define a pointer p outside the function, assign a value to the pointer within the function, and take effect on the pointer p after the end of the function, then we need the second-level pointer.
Let's take a look at the following code: there are two variables, acentine b, and the pointer qmemery Q points to a, and we want Q to point to b, which is implemented in the function.
1. Let's first take a look at the implementation of the first-level pointer.
# includeusing namespace std;int a = 10 int b = 100 int * Q void func (int * p) {cout