有关数组指针和指针数组容易混淆,本文举例说明两者的区别,并加以分析。
基本概念:













分析如下代码:
#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 ]