用一个指针实现二维数组

文章提供三种实现方法,都通过验证 

二维指针

指针数组

数组指针

  #include <cstdlib>
  #include <iostream>
  using namespace std;
  
  const int row = 4;
  const int col = 5;
  
  template <typename T>
  void gen(T **array)
  {
     for(int i=0; i<row; i++)
        for(int j=0; j<col; j++)
     {
         array[i][j] =  i + j;
     }   
     return;
  }
  
  template <typename T>
  void display(T **array)
  {
     for(int i=0; i<row; i++)
     {
         for(int j=0; j<col; j++)
         {
             cout<<array[i][j]<<"   ";
         }  
         cout<<endl;
    } 
    cout<<endl;
    return;
  }
  
  template <typename T>
  void pointpoint(T ** &p)
  {
    p = new T*[row];
    for(int i=0; i<row; i++)
    {
      p[i] = new T[col]; 
    }
  }
  
  template <typename T>
  void Rpointpoint(T ** &p)
  {
    for(int i=0; i<row; i++)
    {
      delete p[i];
      p[i] = NULL; 
    }
    delete p;
    p = NULL;
  }
  
  template <typename T>
  void pointarray(T *p)
  {
    p = new T*[row];
    for(int i=0; i<row; i++)
    {
      p[i] = new T[col]; 
    }
  }
  
  template <typename T>
  void Rpointarray(T ** &p)
  {
    for(int i=0; i<row; i++)
    {
      delete p[i];
      p[i] = NULL; 
    }
    delete p;
    p = NULL;
  }
  
  
  
  int main(int argc, char **argv)
  {
      /* 二维指针的实现 */
      /*
      int **p;
      pointpoint(p);
      gen(p);
      display(p);
      p[2][2] = 110
      p[3][3] = 55;
      display(p);
      Rpointpoint(p);
      */
      
      /* 指针数组的实现 */
      /*
      int *p[row];   // 指针数组的指针 , 也是个二维指针
      for(int i=0; i<row; i++)
      {
         p[i] = new int[col];  // 多个分散的内存块
      }
      gen(p); 
      display(p);
      p[2][2] = 110;
      p[3][3] = 55;
      display(p);
      for(int i=0; i<row; i++)
      {
         delete p[i];
         p[i] = NULL;
      }
      */
      
      /*
      int *p[row];   // 指针数组的指针 , 也是个二维指针
      int *tmp_p = new int[row * col];
      
      for(int i=0; i<row; i++)
      {
         p[i] = tmp_p + i*col;  // 一个分散的内存块
      }
      gen(p); 
      display(p);
      p[2][2] = 110;
      p[3][3] = 55;
      display(p);
      delete p[0];
      */
      
      /* 数组指针的实现 */
      int (*p)[col];
      int *tmp_p = new int[row * col];
      cout<<"Head:"<<tmp_p<<endl;
      p = (int(*)[col]) tmp_p;
  
      for(int i=0; i<row; i++)
      {
         for(int j=0; j<col; j++) cout<<&p[i][j]<<" ";  
         cout<<endl;
      }
      p[2][2] = 110;
      cout<< p[2][2] << endl;
      delete (int*)p;
      
          
      
      return 1;
  }

./test 
Head:0x502010
110
0x502010 0x502014 0x502018 0x50201c 0x502020 
0x502024 0x502028 0x50202c 0x502030 0x502034 
0x502038 0x50203c 0x502040 0x502044 0x502048 
0x50204c 0x502050 0x502054 0x502058 0x50205c 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值