向函数中传输二维数组


void xxx (int **a, int r . int c){  // r代表行 , c代表列

  //在转变后的函数中,array[i][j]这样的式子是不对的,因为编译器不能正确的为它寻址,所以我们需要模仿编译器的行为把array[i][j]这样的式子手工转变为

                                                  ((int *)a + c * (i))[j];

}

    int a[3][3] = 
    {
      {1, 1, 1},
      {2, 2, 2},
      {3, 3, 3}
    };

xxx( (int **)a , 3 ,3 ) ; //强制转换并调用函数

例子 : 打印输出二维数组函数

#include <iostream>
using namespace std;

void print_array ( int**a , int r , int c ) {

  for (int i = 0; i < r; i++)
  {
    for ( int j = 0 ; j<c ; j++)
      {
        cout <<   ((int *)a + c * (i))[j]; ;
        if(j == c-1 ) cout <<endl;
      }
  }
}

int a[3][3] =
{
  {1, 1, 1},
  {2, 2, 2},
  {3, 3, 3}
};

int main(){

print_array ((int **) a ,3,3);

}

原文地址:https://www.cnblogs.com/likeghee/p/9955982.html