Ann
Ann

Reputation: 53

error in passing the 2D array to a function in c++

unusual error in function solve when I pass the dp array to the function.

int n,k;
int solve(int n, int k, int dp[n+1][k+1]) 
{ 
  // some code
} 
int main(){
  int t; cin>>t;
  while(t--){
     cin>>n;
     cin>>k;
     int dp[n+1][k+1];
    memset(dp, -1,sizeof(dp));
    cout<<solve(n,k,dp)<<endl;
  }
return 0;
}

why this error: use of parameter outside function body before '+' token int solve(int n, int k, int dp[n+1][k+1]) I am not able to understand why is this error

Upvotes: 0

Views: 103

Answers (2)

Jose Garza
Jose Garza

Reputation: 3

In C++, the array's size can't be changed dynamically. Granted you are reading in the sizes, this will cause issues. You can hard code the value of n,k for testing purposes

int dp[10][10];
int solve(int n, int k, int dp[10][10])

or attempt using a vector instead.

Upvotes: 0

john
john

Reputation: 87959

In C++ array sizes must be compile time constants.

This is not legal C++

int dp[n+1][k+1];

because n and k are variables not constants.

And this is not legal C++

int solve(int n, int k, int dp[n+1][k+1]) 

for exactly the same reason.

Upvotes: 2

Related Questions