c-引用函数中的矩阵来计算邻接矩阵中节点的阶数的错误


int nodeInDegree(int *g, int node){
int i;
int count=0;
for(i=0; i<COLUMNS; i++){
if (g[node][i] == 1){
++count;
}
}
return count;
}

上面的代码一直给我一个g[node][i]的错误,说";下标值不是数组、指针或向量";

如果要将数组作为参数(g(发送,则需要发送一个2d数组或矩阵,将参数从int*g更改为int**g

编辑:我相信你需要的是这条

#include <stdio.h>
int nodeInDegree(int *g)
{
int i;
int count = 0;
for (i = 0; i < 5; i++)
if (g[i] == 1) ++count;
return count;
}
int main()
{
int mat[5][5] = {{1, 0, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 0, 1, 0, 0},
{1, 0, 1, 0, 0}};
printf("%dn", nodeInDegree(&mat[0]));
return 0;
}

最新更新