C - 不能将1个参数从int转换为int [] [4]

时间:2017-03-02 12:30:40

标签: c arrays function

我在使用C编译器时遇到错误,我无法做任何事情,我在网上浏览解决方案但未能解决问题

第一个错误: error C2664: 'int get_min_distance(int *[][4],int,int,int,int)' : cannot convert argument 1 from 'int' to 'int *[][4]'

第二个错误:IntelliSense: argument of type "int" is incompatible with parameter of type "int (*)[4]"

两个错误都在一行

代码:

#include<stdio.h>
#define inf  100000
int get_min_distance(int a[4][4], int b, int c, int d, int e);



int main(){
    int ras = 0;
    scanf("%d", &ras);

    int yal = 0;
    scanf("%d", &yal);

    int mabda = 0;
    scanf("%d", &mabda);

    int magsad = 0;
    scanf("%d", &magsad);


    int gf[4][4];
    gf[0][0] = 0;
    gf[0][1] = 1;
    gf[0][2] = inf;
    gf[0][3] = 1;
    gf[0][4] = 5;
    gf[1][0] = 9;
    gf[1][1] = 0;
    gf[1][2] = 3;
    gf[1][3] = 2;
    gf[1][4] = inf;
    gf[2][0] = inf;
    gf[2][1] = inf;
    gf[2][2] = 0;
    gf[2][3] = 4;
    gf[2][4] = inf;
    gf[3][0] = inf;
    gf[3][1] = inf;
    gf[3][2] = 2;
    gf[3][3] = 0;
    gf[3][4] = 3;
    gf[4][0] = 3;
    gf[4][1] = inf;
    gf[4][2] = inf;
    gf[4][3] = inf;
    gf[4][4] = 0;



    int distance = get_min_distance(gf [4][4], ras, yal, mabda, magsad);     //Error is here on "gf [4][4]"
    printf("%d", distance);


}


int get_min_distance(int gf[4][4], int ras, int yal, int mabda, int magsad){

    int min = inf;
    int i = ras;
    int A[5];

    if (mabda == magsad){
        return 0;
    }
    if (yal >= ras - 1){
        return inf;
    }
    for (int i = 0; i < ras; i++){
        if (gf[i][magsad] != 0 && gf[i][magsad] != inf){
            A[i] = gf[i][magsad] + get_min_distance(gf, ras, yal + 1, mabda, i);
            if (min>A[i] && A[i] > 0){
                min = A[i];
            }
        }
    }
    return min;
}
当我们调用get_min_distance函数时会发生

错误 任何的想法? 感谢

2 个答案:

答案 0 :(得分:2)

提供数组的名称作为参数

int distance = get_min_distance(gf, ras, yal, mabda, magsad);     
                               ^^^ 

此阵列初始化也无效

int gf[4][4];
gf[0][0] = 0;
gf[0][1] = 1;
gf[0][2] = inf;
gf[0][3] = 1;
gf[0][4] = 5;
     ^^^
gf[1][0] = 9;
gf[1][1] = 0;
gf[1][2] = 3;
gf[1][3] = 2;
gf[1][4] = inf;
     ^^^
gf[2][0] = inf;
gf[2][1] = inf;
gf[2][2] = 0;
gf[2][3] = 4;
gf[2][4] = inf;
     ^^^
gf[3][0] = inf;
gf[3][1] = inf;
gf[3][2] = 2;
gf[3][3] = 0;
gf[3][4] = 3;
     ^^^
gf[4][0] = 3;
  ^^^
gf[4][1] = inf;
  ^^^
gf[4][2] = inf;
  ^^^
gf[4][3] = inf;
  ^^^
gf[4][4] = 0;
  ^^^^^^

因为索引的有效范围是[0, 3]

答案 1 :(得分:1)

gf[4][4]gf数组

的整数(超出范围)

只需传递gf变量:

get_min_distance(gf, ras, yal, mabda, magsad)

注意:这是错误的,也是错误的,你的数组太小了。 [4][4]表示最大合法索引为3,否则您将覆盖您不拥有的内存:

gf[4][0] = 3;
gf[4][1] = inf;
gf[4][2] = inf;
gf[4][3] = inf;
gf[4][4] = 0;

(您的维度可能意味着[5][5]