指针数组和数组指针

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

基本概念:

 

------------------- 指针 ---------------------
int  a = 1 ;
int   * p =& a;
---------------- 指针的指针 ------------------
int   ** p2p =& p;
----------------- 简单数组 --------------------
int  b[ 20 ];
----------------- 指针数组 --------------------
int   * p[ 10 ];       // 指针数组,含有10个指针元素,
                 
// 即每一个元素都是一个指针
----------------- 数组指针 ---------------------
int  ( * p)[ 10 ];     // 这个指针用来指向含有10个整数
                 
// 元素的数组

 分析如下代码:

 

#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 ;
}

输出结果:

 

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 ]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值