Detailed explanation of secondary pointer function

Concept:

In the following pointing relationship of A to B and B to C:

First, C is "a piece of content". For example, 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 in which the address of C is stored, 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's memory, so the content in B is 0x00000008.

B= 0x00000008;  //B Content of 
*B = "A paragraph";  //B Dereference, that is B Pointer pointing C Value of
&B = 0x00000004;  //B Take the address, B Your address is 0 x00000004

Then, let's look at A:

A is a secondary pointer variable, in which the address 0x00000004 of B is stored, and a also has an address, 0x00000000;

*A = B= 0x00000008;  //A Dereference is B Content of 
**A = *B = "A paragraph";  //B Dereference, that is B Pointer pointing C Value of
A = &B = 0x00000004;  //A Save is B Your address, B Your address is 0 x00000004
&A = 0x00000000;  //A Take address

That is, secondary pointer A points to primary pointer B and primary pointer B points to C. Focus on understanding the pointer address, pointer content and pointer content.

 

The following code: there are two variables a and b, and the pointer q points to a. We want q to point to b and implement it in the function.

Example 1: implementation of primary pointer

#include<iostream>
using namespace std; 
int a= 10;
int b = 100;
int *q;
 
void func(int *p)
{
    cout<<"func:&p="<<&p<<",p="<<p<<endl;  //note:3
    p = &b;
    cout<<"func:&p="<<&p<<",p="<<p<<endl;  //note:4
}
 
int main()
{
    cout<<"&a="<<&a<<",&b="<<&b<<",&q="<<&q<<endl;  //note:1
    q = &a;
    cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl;  //note:2
    func(q);
    cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl;  //note:5
 
    system("pause");
    return 0;
}

Output result:

&a=0032F000,&b=0032F004,&q=0032F228
*q=10,q=0032F000,&q=0032F228
func:&p=0018FD24,p=0032F000
func:&p=0018FD24,p=0032F004
*q=10,q=0032F000,&q=0032F228

Let's look at the output:

Note: 1 - > A, B, Q all have an address

Note: 2 - > Q points to a

Note: 3 - > we find that the address of parameter p has changed, which is different from that of q. yes, a copy is made for parameter passing, that is, p and q are not the same pointer, but the address 0x0032f000 (the address of a) is still unchanged

Note: 4 - > p re point to b

Note: 5 - > exit the function, and the modification of p will not affect q.

Conclusion:

The compiler always makes a temporary copy for each parameter of the function. The copy of pointer parameter p is p, and the compiler makes p = q (but & P! = & Q, that is, they are not at the same memory address, but their contents are the same, they are all the addresses of a). If the program in the function body modifies the content of P (for example, it points to b here). In this example, P applies for new memory, but changes the memory address referred to by P (into the address of b, but the memory address pointed to by Q has no effect), so it does not affect the pointer Q outside the function here.

This requires secondary pointer operation:

Example 2, implementation of secondary pointer

#include<iostream>
 
using namespace std;
 
int a= 10;
int b = 100;
int *q;
 
void func(int **p)  //2
{
    cout<<"func:&p="<<&p<<",p="<<p<<endl;
    *p = &b;  //3
    cout<<"func:&p="<<&p<<",p="<<p<<endl;
}
 
 
int main()
{
    cout<<"&a="<<&a<<",&b="<<&b<<",&q="<<&q<<endl;
    q = &a;
    cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl;
    func(&q);  //1
    cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl;
 
    system("pause");
    return 0;
}

Only three places have been changed here to pass the secondary pointer. Let's look again:

Because the address of pointer Q (secondary pointer * * p) is passed to the function, the secondary pointer is copied (p is copied, and Q is copied in the primary pointer, so there is a problem), (the pointer is copied, but the pointer content, that is, the address pointed to by the pointer, is unchanged), so it still points to the primary pointer q(*p = q). Here, no matter how many times it is copied, it still points to Q, then * p = & B; Naturally, q = & B; Yes.

 

use:

The function of the secondary pointer as a function parameter: define a pointer p outside the function, assign a value to the pointer in the function, and take effect on the pointer P after the function is completed, so we need the secondary pointer (the reason for using the secondary pointer).

Tags: C++

Posted by cturner on Thu, 12 May 2022 04:23:03 +0300