在说指向指针的指针之前,不得不说指向变量的指针。先看如下示例:
1. 指向整型指针
先看如下示例:
1 #include <iostream> using namespace std; int main() { int a = 5; int * p = &a; cout << "a = " << a << endl << "&a = " << &a << endl << "*p = " << *p << endl << "p = " << p << endl << "&p = " << &p << endl; return 0; }
运行结果如下:
我们先看下内存分配图:
由上图可以清楚的知道,输出整形变量a的值是5,指针变量p的值是001BFD18,而*号的作用是取值,*p即取地址001BFD18中存放的值,即5。
2. 指向字符型指针
先看如下示例:
1 #include <iostream> using namespace std; int main() { char a[] = "hello"; char *p = a; cout << "p = " << p << endl <<"p = " << (void *) p << endl << "*p = " << *p << endl; for(int i = 0 ; i < 5; i++) { cout << "&a[" << i << "] = "<< (void *)&a[i] << endl; } return 0; }
2 using namespace std;
3
4 int main()
5 {
6 char a[] = "hello";
7 char *p = a;
8
9 cout << "p = " << p << endl
10 <<"p = " << (void *) p << endl
11 << "*p = " << *p << endl;
12
13
14 for(int i = 0 ; i < 5; i++)
15 {
16 cout << "&a[" << i << "] = "<< (void *)&a[i] << endl;
17 }
18 return 0;
19 }
运行结果图如下:
为什么整型指针p输出的是地址,而字符型指针输出的是字符串呢,字符型指针里存放的不是地址吗?
我们先看下内存分配图:
由上图可以看出,其实p中存放的是地址,只是当cout时,如果指针是字符型指针,那么会输出p中地址指向的内存中的内容(这里是h)直到遇到' '才结束。所以直接输出p时会输出hello,而将p强制转换为void *时输出的是地址。
3. 指向整型指针的指针
先看如下示例:
1 #include <iostream> using namespace std; int main() { int a[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 (int i = 0; i < 5; i++) { cout << "&a[" << i << "] = " << &a[i] << endl; } return 0; }
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
10 cout << "a = " << a << endl
11 << "p = " << p << endl
12 << "&p = " << &p << endl
13 << "point = " << point << endl
14 << "&point = " << &point << endl;
15
16 for (int i = 0; i < 5; i++)
17 {
18 cout << "&a[" << i << "] = " << &a[i] << endl;
19 }
20 return 0;
21 }
运行结果图如下:
我们先看下内存分配图:
从上图可以看出point指针中存放的是p指针的地址,而p指针中存放的是a[0]的地址。所以*point和p是一样的,前者是取point指针中存放的地址(0025F754)中的值,即取地址0025F754中存放的值(0025F760),而后者就是0025F760,所以两者是等价的。**point和a[0]是等价的,前者可以写成*p,*p是取p中存放的地址(0025F760)中的值,即地址0025F760中存放的值1。由上可以得出*point等于p, **point 等于 a[0]。通过上图可以清晰的对付诸如*point++等问题。
4. 指向整型指针的指针
先看如下示例:
1 #include <iostream> using namespace std; int main() { char *a[] = {"Wel", "To", "China"}; char **p = a; for(int i = 0; i < 3; i++) { for (int j = 0; j < strlen(a[i]) + 1; j++) { cout << a[i][j] << " " << (void *)&a[i][j] << endl; } cout << endl; } for (int i = 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; }
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 {
10 for (int j = 0; j < strlen(a[i]) + 1; j++)
11 {
12 cout << a[i][j] << " " << (void *)&a[i][j] << endl;
13 }
14 cout << endl;
15 }
16
17 for (int i = 0; i < 3; i++)
18 {
19 cout << "a[" << i << "] = " << (void *) a[i] << endl
20 << "&a[" << i << "] = " << &a[i] << endl;
21 }
22
23
24 cout << "p = " << p << endl
25 << "&p = " << &p << endl;
26 return 0;
27 }
运行结果图如下:
我们先看下内存分配图:
由上图可以看出a[0]中存放着'W'的地址,a[1]中存放着'T'的地址,a[2]中存放着'C'的地址,只是这些地址都是指向字符型的,所以直接cout的会输出字符串,而指针p中存放着a[0]的地址,所以*p等于a[0],都是获得'W'的地址,即00A778CCC,而**p和a[0][0]等价都获得了地址00A778CCC中存放的值W。由上图我们可以看到字符地址相隔1个字节,而指针地址相隔4个字节,这样就便于++运算,获得下一个地址了,列如++p后,p就指向a[1],p中存放的是a[1]的地址。