gpt4 book ai didi

c - 在纯 C 中传递二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:37 26 4
gpt4 key购买 nike

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

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

主要功能是

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

我如预期的那样得到了

error C2664: 'reverse' : cannot convert parameter 1 from 'int [7][7]' to 'int **'

我们能做些什么吗?

我们可以做的最好的事情是访问传递的二维数组的ij(将v作为一维数组传递)是

v[vertexes*i][j]

最佳答案

就用

void reverse(int vertexes, int v[vertexes][vertexes])

尝试使用 int ** 不会立即使用内置二维数组。如果你想坚持使用 int ** 接口(interface),那么你必须创建一个额外的临时行索引数组,如此处所述

Passing two-dimensional array via pointer

或在这里

conversion of 2D array to pointer-to-pointer

关于c - 在纯 C 中传递二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com