A pointer has always been a confusing thing. For a pointer, its own address is its essential address, and its value is the address of the variable it points. You can change the pointer to the variable value.
For example, intm = 4; int * p; p = & m; the value saved in the address of the pointer P is the address of m.
For the first-level pointer intm = 4; intn = 5int * p; p = & m; int * q; q = p; q and p both point to the variable m, you can modify the values of P and q to modify the m value of the variable, but q cannot change the point of P to point it to Variable n. To achieve this, you must use a second-level pointer.
For the second-level pointer intm = 4; intn = 5int * p; p = & m; int ** q; q = & p; the second-level pointer can not only change the value of the variable to which it points, you can also change the point of a level-1 pointer. The second-level pointer ** q * stores the address of the pointer p, and ** q stores the value of * p as the address of the Variable m, in this way, you can use ** q to change the m value of the variable pointed to by * p. For example, int * z; z = & n; by modifying * q = z;, the pointer p is changed so that it does not point to m, but to n.That is to say, a level-2 pointer can not only change the value of the variable to which it points, but also change the point of the level-1 pointer to other variables.
Example:
Example 1: The input parameter is a pointer to the pointer.
# Include <iostream>
Usingnamespacestd;
Intnum = 8;
VoidChangePoint (int ** p2) // The input parameter type is pointer to the pointer.
{
* P2 = & num; // * If p2 is the address of p1, p1 points to num.
}
Intmain ()
{
Inta = 5;
Int * p1 = &;
ChangePoint (& p1); // point to num through function p1, and the value of a is still 5.
Cout <* p1 <''<;
}
Output:
85
Example 2: The input parameter is a pointer.
# Include <iostream>
Usingnamespacestd;
Intnum = 8;
VoidChangePoint (int * p2) // If the input parameter type is pointer, p2 and p1 are two pointers, with the two pointers pointing to the same address.
{
Inti = 0;
Int * p3 = & I;
* P2 = num; // If p2 points to the content, the value of p1 points to the content is changed, and the value of a changes to 8.
P2 = p3; // the p2 pointer variable does not affect the point of p1.
}
Intmain ()
{
Inta = 5;
Int * p1 = &;
ChangePoint (p1); // p1 still points to a, but the value of a has changed to 8.
Cout <* p1 <''<;
}
Output:
88
# Include <iostream> using namespace std; # pragma argsusedvoid ModifyPointValue (int * p, int ** q, int * z) {int a = 10; // only change the point of Z without changing the value of K z = & a; // change the value of variable I pointed to by p * p = 10; // The second-level pointer * q is the address of the pointer q. ** q points to the variable j, and the value of j points to can be changed, // you can also change the direction of q so that it does not point to the variable j ** q =; cout <"In function value is" <"P Value is" <* p <"q value is" <** q <endl ;} int main (int argc, char * argv []) {int I = 4; int j = 5; int k = 6; int * p = NULL; int * q = NULL; int * z; p = & I; q = & j; // z points to the variable jz = q together with q; cout <"z is" <* z <"q is" <* q <endl; cout <"I is" <I <"j is" <j <"k is" <k <endl; // modified the value of variable j * z = 8; // The value of Z points to kz = & k instead of j; cout <"I is" <I <"j is" <j <"k is" <k <endl; cout <"z is" <* z <"q is" <* q <endl; cout <"Befor Function P Value is" <* p <"q value is" <* q <endl; cout <"I is" <I <"j is" <j <"k is" <k <endl; ModifyPointValue (p, & q, z); cout <"After Function P Value is" <* p <"q value is" <* q <endl; cout <"I is" <I <"j is" <j <"k is" <k <endl; char c = getchar (); cout <"Pree any key exit ..... "<endl; return 0 ;}//---------------------------------------------------------------------------
650) this. width = 650; "title =" qq 30929000307.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/19353SA7-0.jpg "alt =" 000349656.jpg"/>