在纯C中传递二维数组

set*_*thi 0 c arrays graph

我正在尝试编写通用矩阵转置函数

void reverse(int** v , int vertexes )
{
    for(int i=0;i<vertexes;i++)
        for(int j=0;j<vertexes;j++) 
        {
            if(v[i][j] == 1 && v[j][i]==0){
                v[j][i] = -1;
                v[i][j] = 0;        
            }
        }

    for(int i=0;i<vertexes;i++)
        for(int j=0;j<vertexes;j++) 
        {
            if(v[i][j] == -1 )
                v[i][j] = 1;
        }
} 
Run Code Online (Sandbox Code Playgroud)

而主要功能是

void matrix_graph::process()
{

    int v[7][7] = {
        {0,1,0,0,0,0,0},
        {0,0,1,1,0,0,0},
        {1,0,0,0,0,0,0}, 
        {0,0,0,0,1,0,0},
        {0,0,0,0,0,1,0},
        {0,0,0,1,0,0,1},
        {0,0,0,0,0,1,0}
    };

    reverse(v,7);
}
Run Code Online (Sandbox Code Playgroud)

我按预期得到了一个

error C2664: 'reverse' : cannot convert parameter 1 from 'int [7][7]' to 'int **'
Run Code Online (Sandbox Code Playgroud)

我们能做些什么吗?

我们可以做的最好的访问i,j传递v的二维数组(作为一维数组传递)是

v[vertexes*i][j]
Run Code Online (Sandbox Code Playgroud)

AnT*_*AnT 5

只是用

void reverse(int vertexes, int v[vertexes][vertexes])
Run Code Online (Sandbox Code Playgroud)

尝试使用int **不会立即使用内置的2D数组.如果你想坚持使用int **接口,那么你将不得不创建一个额外的临时行索引数组,如下所述

通过指针传递二维数组

或者在这里

将2D数组转换为指针指针