Before you say a pointer to a pointer, you have to say a pointer to the variable. Let's look at the following example:
1. Pointer to integral type
Let's look at the following example:
1#include <iostream>using namespacestd;intMain () {intA =5; int* p = &A; cout<<"A ="<< a <<Endl<<"&a ="<< &a <<Endl<<"*p ="<< *p <<Endl<<"p ="<< P <<Endl<<"&p ="<< &p <<Endl;return 0;}
The results of the operation are as follows:
Let's look at the memory allocation diagram first:
It can be clearly known that the value of the output shaping variable A is 5, the value of the pointer variable p is 001BFD18, and the function of the * is to take a value, *p is the address 001bfd18 the value stored in, that is, 5.
2. Pointer to character type
Let's look at the following example:
1#include <iostream>using namespacestd;intMain () {CharA[] ="Hello"; Char*p =A; cout<<"p ="<< P <<Endl<<"p ="<< (void*) P <<Endl<<"*p ="<< *p <<Endl; for(inti =0; I <5; i++) {cout<<"&a["<< I <<"] = "<< (void*) &a[i] <<Endl; } return 0;}
1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 char a[] = "Hello";
7 char *p = A;
8
9 cout << "p =" << p << Endl
<< "p =" << (void *) P << Endl
<< "*p =" << *p << Endl;
12
13
+ for (int i = 0; i < 5; i++)
15 {
cout << "&a[" << i << "] =" << (void *) &a[i] << Endl;
17}
return 0;
19}
Run the result diagram as follows:
Why is the integer pointer p output the address, and the character pointer output is a string, the character pointer is not stored in the address?
Let's look at the memory allocation diagram first:
As can be seen, in fact, p is stored in the address, but when cout, if the pointer is a character pointer, then the address in the p in the memory of the contents (here is h) until the end of the encounter '. So the direct output p will output Hello, and the P cast to void * When the output is the address.
3. Pointer to an integer pointer
Let's look at the following example:
1#include <iostream>using namespacestd;intMain () {inta[5] = {1,2,3,4,5}; int*p =A; int**point = &p; cout<<"A ="<< a <<Endl<<"p ="<< P <<Endl<<"&p ="<< &p <<Endl<<"Point ="<< Point <<Endl<<"&point ="<< &point <<Endl; for(inti =0; I <5; i++) {cout<<"&a["<< I <<"] = "<< &a[i] <<Endl; } return 0;}
1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 int A[5] = {1, 2, 3, 4, 5};
7 int *p = A;
8 int **point = &p;
9
Ten cout << "a =" << a << Endl
<< "p =" << p << Endl
<< "&p =" << &p << Endl
<< ' point = ' << point << Endl
<< "&point =" << &point << Endl;
15
(int i = 0; i < 5; i++)
17 {
cout << "&a[" << i << "] =" << &a[i] << Endl;
19}
return 0;
21}
Run the result diagram as follows:
Let's look at the memory allocation diagram first:
You can see that the address of the P pointer is stored in the point pointer, and the address of a[0] is stored in the P pointer. So *point and P are the same, the former is the value in the address (0025f754) stored in the point pointer, which is the value stored in the address 0025f754 (0025f760), and the latter is 0025f760, so the two are equivalent. **point and a[0] are equivalent, the former can be written *p,*p is to take the address stored in P (0025f760) value, that is, the value of the address 0025f760 stored in 1. It can be concluded that *point equals p, **point equals a[0]. By being able to deal clearly with issues such as *point++.
4. Pointer to an integer pointer
Let's look at the following example:
1#include <iostream>using namespacestd;intMain () {Char*a[] = {"Wel"," to"," China"}; Char**p =A; for(inti =0; I <3; i++) { for(intj =0; J < strlen (A[i]) +1; J + +) {cout<< A[i][j] <<"\ t"<< (void*) &a[i][j] <<Endl; } cout<<Endl; } for(inti =0; I <3; i++) {cout<<"a["<< I <<"] = "<< (void*) A[i] <<Endl<<"&a["<< I <<"] = "<< &a[i] <<Endl; } cout<<"p ="<< P <<Endl<<"&p ="<< &p <<Endl; return 0;}
1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 char *a[] = {"Wel", "to", "China"};
7 char **p = A;
8 for (int i = 0; i < 3; i++)
9 {
Ten for (int j = 0; J < strlen (A[i]) + 1; j + +)
11 {
cout << a[i][j] << "\ t" << (void *) &a[i][j] << Endl;
13}
cout << Endl;
15}
16
(int i = 0; i < 3; i++)
18 {
cout << "a[" << i << "] =" << (void *) A[i] << Endl
<< "&a[" << i << "] =" << &a[i] << Endl;
21}
22
23
cout << "p =" << p << Endl
<< "&p =" << &p << Endl;
return 0;
27}
Run the result diagram as follows:
Let's look at the memory allocation diagram first:
It can be seen that the address of ' W ' is stored in a[0], the address of ' T ' is stored in a[1], the address of ' C ' is stored in a[2], except that these addresses are pointing to the character type, so the direct cout output string, and the address of the pointer p is stored in a[0], so *p equals a[0], All are obtained the ' W ' address, that is, 00A778CCC, and **p and a[0][0] equivalent are obtained in the address 00A778CCC the value stored in the W. We can see that the character address is 1 bytes apart, and the pointer address is 4 bytes apart, so that it is convenient for the + + operation, get the next address, after columns such as ++P, p points to the address of the a[1],p is stored in a[1].
Understanding general pointers and pointers to pointers