运行此代码时,我得到:

prog.cpp:67:33: error: cannot convert ‘int (*)[n][m]’ to ‘int
 (*)[100000]’ for argument ‘4’ to ‘int calculateP(int, int, int, int
 (*)[100000])’
       cout << calculateP(l,n,m,&a) << endl;

我尝试做很多事情,但它们无济于事,该错误仍然存​​在。希望快速获得有关如何消除此错误的信息。
#include <iostream>
using namespace std;


int flr(int k){
    return (k-(k%2))/2;
}

int getPalindrome(int l, int x , int y, int b[100000][100000]){
    int num = 0;
    if(b[x-1][y] == b[x][y-1]){
        for(int n = 2; n < l+2; n+=2){
            if(b[x-n][y] != b[x][y-n]){
                return num;
            }else{
                if(b[x+n][y] != b[x][y+n]){
                    return num;
                }else{
                    num ++;
                }
            }
        }
        return num;}
    else if(b[x-1][y] == b[x][y+1]){
        for(int n = 2; n < l+2; n+=2){
            if(b[x-n][y] != b[x][y+n]){
                return num;
            }else{
                if(b[x+n][y] != b[x][y-n]){
                    return num;
                }else{
                    num ++;
                }
            }
        }
        return num;
}
    else{return num;}
}


int calculateP(int l, int n, int m, int a[][100000]){
    int pairs = n*m;
    for(int x = l; x < n-l; x++){
        for(int y = l; y < m-l; y++){
            pairs += getPalindrome(l,x,y,a);
        }
    }
    return pairs;
}


int main() {
    int t;
    cin >> t;
    while(t--){
        int n, m, l;
        cin >> n >> m;
        if(n < m){l = flr(n-1);}
        else{l = flr(m-1);}
        int a[n][m];
        for(int i = 0; i< n; i++){
            for(int j = 0; j< m; j++){cin >> a[i][j];}
        }

        cout << calculateP(l,n,m,a) << endl;
    }

}

最佳答案

使用2d数组作为函数参数应该做的不同;

查看此正确的代码,然后尝试像这样更改您的代码:

#define row_max 10
#define col_max 10

//you must know the size of array (row_max , col_max)
void foo(int (*mArray)[row_max][col_max]){

    //work with array this way :
    (*mArray)[2][2] = 10;

}
void main(void){
        int myarray[row_max][col_max] = {0};
        foo(&myarray);
}

08-05 01:53