hemant mishra
hemant mishra

Reputation: 51

Passing 2D array as an argument

I am running my code but getting this error and I don't know how to deal with it.

My code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define LIMIT 5
int m;

void dfs(int i,int j,int k,int kk,int mat[][1000],bool vis[101][101],int n,int m) {
    if(i<0 || i>n-1 || j<0 || j>m-1)
        return;
    if(mat[i][j]!=kk)
        return;
    if(vis[i][j])
        return;
    mat[i][j] = k;
    dfs(i,j+1,k,kk,mat,vis,n,m);
    dfs(i,j-1,k,kk,mat,vis,n,m);
    dfs(i+1,j,k,kk,mat,vis,n,m);
    dfs(i+1,j+1,k,kk,mat,vis,n,m);
    dfs(i+1,j-1,k,kk,mat,vis,n,m);
    dfs(i-1,j,k,kk,mat,vis,n,m);
    dfs(i-1,j-1,k,kk,mat,vis,n,m);
    dfs(i-1,j+1,k,kk,mat,vis,n,m);
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n,m;
        bool vis[101][101];
        cin>>n>>m;
        int mat[101][101];
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                cin>>mat[i][j];
        int x,y,k;
        cin>>x>>y>>k;
        int kk = mat[x][y];

        dfs(x,y,k,mat,vis,n,m);
    }
    return 0;
}

The error:

cpp:41:13: error: invalid conversion from ‘int (*)[101]’ to ‘int’ [-fpermissive]
41 |   dfs(x,y,k,mat,vis,n,m);
   |             ^~~
   |             |
   |             int (*)[101]

Thanks in advance.

Upvotes: 0

Views: 84

Answers (2)

anastaciu
anastaciu

Reputation: 23792

You have a typo in the function call, it's missing a parameter, 4 int parameters are necessary before the 5th parameter, a 2D array, I'm guessing you forgot to include the 4th parameter kk. That what's causing that compilation error.

Fixing that won't solve your problem because you can't pass an array with dimensions (*)[101] to a function that receives a parameter with (*)[1000] dimensions, they must match, these are regarded as incompatible types.

Either declare mat as:

 int mat[1000][1000];

Or change the function declaration to:

void dfs(int i, int j, int k, int kk, int mat[][101], bool vis[101][101], int n, int m)
                                                ^^^

Upvotes: 2

Eray
Eray

Reputation: 1

There's another int parameter (kk) in your dfs function, but you are ignoring it in line 41: dfs(x,y,k,mat,vis,n,m);, you should write another integer value after k

Upvotes: 0

Related Questions