Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing 2D array with variable dimensions as function argument

I just saw the following code among the successful submissions at codechef.

http://www.codechef.com/viewplaintext/1595846

I used to think that

float max(int n,int arr[n][n])
{....}

is not allowed in C++ (as 'n' is a variable). My CodeBlocks (on windows) with MinGW [gcc 4.4] gives compile time error. that "error: array bound is not an integer constant.

Then how can be such a solution be accepted by CodeChef's judge. Is there any special flag that allows us to do that in C++???

EDIT: A link showing status as AC (accepted) : http://www.codechef.com/viewsolution/1595846

like image 972
TheCrazyProgrammer Avatar asked Dec 11 '12 10:12

TheCrazyProgrammer


People also ask

How do you pass a 2D array of variable size?

Most code and explanations are for a constant size of the array. In my function A I declare the variable and then I manipulate it a bit, and then it must be passed to Function B . You pass it as a variable to the function, like you have done in the question.

Can you pass multidimensional array to a function?

In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples. In C++, we can pass arrays as an argument to a function. And, also we can return arrays from a function.


1 Answers

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression.

Ref: http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

like image 140
Abhishek Thakur Avatar answered Oct 27 '22 19:10

Abhishek Thakur