有关数组指针和指针数组容易混淆,本文举例说明两者的区别,并加以分析。
基本概念:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
分析如下代码:
#include
<
iostream
>
#include < typeinfo >
using namespace std;
int main()
{
int a = 10 ;
int b[ 2 ] = { 6 , 8 };
int * p =& a;
int ** p2p =& p;
int * pb[ 2 ] = { & a, & a};
int ( * p2b)[ 2 ] =& b;
cout << " declaration [int a=10] type== " << typeid(a).name() << endl;
cout << " declaration [int b[2]={6,8}] type== " << typeid(b).name() << endl;
cout << " declaration [int *p=&a] type== " << typeid(p).name() << endl;
cout << " declaration [int **p2p=&p] type== " << typeid(p2p).name() << endl;
cout << " declaration [int *pb[2]={&a,&a}] type== " << typeid(pb).name() << endl;
cout << " declaration [int (*p2b)[2]=&b] type== " << typeid(p2b).name() << endl;
return 0 ;
}
#include < typeinfo >
using namespace std;
int main()
{
int a = 10 ;
int b[ 2 ] = { 6 , 8 };
int * p =& a;
int ** p2p =& p;
int * pb[ 2 ] = { & a, & a};
int ( * p2b)[ 2 ] =& b;
cout << " declaration [int a=10] type== " << typeid(a).name() << endl;
cout << " declaration [int b[2]={6,8}] type== " << typeid(b).name() << endl;
cout << " declaration [int *p=&a] type== " << typeid(p).name() << endl;
cout << " declaration [int **p2p=&p] type== " << typeid(p2p).name() << endl;
cout << " declaration [int *pb[2]={&a,&a}] type== " << typeid(pb).name() << endl;
cout << " declaration [int (*p2b)[2]=&b] type== " << typeid(p2b).name() << endl;
return 0 ;
}
输出结果:
declaration [
int
a
=
10
] type
==
int
declaration [ int b[ 2 ] = { 6 , 8 }] type == int [ 2 ]
declaration [ int * p =& a] type == int *
declaration [ int ** p2p =& p] type == int * *
declaration [ int * pb[ 2 ] = { & a, & a}] type == int * [ 2 ]
declaration [ int ( * p2b)[ 2 ] =& b] type == int ( * )[ 2 ]
declaration [ int b[ 2 ] = { 6 , 8 }] type == int [ 2 ]
declaration [ int * p =& a] type == int *
declaration [ int ** p2p =& p] type == int * *
declaration [ int * pb[ 2 ] = { & a, & a}] type == int * [ 2 ]
declaration [ int ( * p2b)[ 2 ] =& b] type == int ( * )[ 2 ]