passing 2d array in c 2b 2b in function

Solutions on MaxInterview for passing 2d array in c 2b 2b in function by the best coders in the world

showing results for - "passing 2d array in c 2b 2b in function"
Ernie
10 May 2020
1If you are using GCC, then you are to pass the length of the array as a parameter to the function.
2
3void foo (int m, int arr[][m]) {
4    //...
5}
6However, there seems to be a bug in either the compiler or the documentation, as the above function prototype syntax only works when compiling C code, not C++ (as of gcc version 4.8.2). The only work-around I found was to use a void * parameter, and cast it int the function body:
7
8int foo_workaround (int m, void *x)
9{
10    int (*arr)[m] = static_cast<int (*)[m]>(x);
11    //...
12}
13There are other solutions if you do not want to rely on a compiler extension. If you don't mind a separate allocation for each row, you can use a vector of vectors, for example:
14
15std::vector<std::vector<int> > arr(n, std::vector<int>(m));
queries leading to this page
cpp pass 2d array in functionc 2b 2b 2d array in function parameterpass 2d array to functionhow to pass 2d array in c 2b 2bhow to pass 2d array to function in c 2b 2bc 2b 2b 2d array as parameterpass 2d array in functionpassing a 2d array as a function parameterhow to pass 2d array in function c 2b 2bc 2b 2b how to use 2d array as parameterhow to pass 2d array in function2d array in function c 2b 2bpassing 2d matrix in cpppass 2d matrix to function in c 2b 2bhow to pass variable length 2d array to function in c 2b 2bhow to pass 2d array to function c 2b 2bhow to pass a 2d array to a functionsend 2d array to function c 2b 2bc 2b 2b pass 2d matrixpassing 2d array as parameter c 2b 2bc 2b 2b pass 2d array to functionpassing 2d vector to a function c 2b 2bhow to call 2d array in function cpppassing 2d matrix to a function c 2b 2bpass 2d array as function argument in cpppassing a 2d array to a function in c 2b 2b passsing by value 2d array c 2b 2bpass 2d dynamic array to function c 2b 2bpass 2d array to function c 2b 2bhow to pass 2d dynamic array to function c 2b 2bhow to pass an 2d array to a function c 2b 2b2d array as a parameter in c 2b 2bhow to pass 2d array as argument in c 2b 2bpassing 2d array to funciton in2d array parameter c 2b 2b2d array as parameter c 2b 2bhow to write 2d array in argument c 2b 2bpassing 2d array to function in cpp2d array as argument to function c 2b 2btypes of ways to pass 2d array to a function in c 2b 2bpass 2d array c 2b 2bpassing an 2d array to an functionc 2b 2b 2d array in functionpassing 2d array to function parameter example in c 2b 2bhow to pass two dimensional array to function in c 2b 2bhow to use a 2d array as a parameter in c 2b 2bcpp passing 2d array to functionpassing 2d array to function parameterhow to pass two dimensional array to function in c 2b 3dc 2b 2b 2d array passingpass dynamic 2d array to function c 2b 2bhow to pass 2d array to function in cppc 2b 2b passing 2d array to functionpass 2d array as reffernce c 2b 2b2d array passing c 2b 2bhow to pass 2d arrays as parameters c 2b 2bpassing 2d array to function in c 2b 2b by referencepassing 2d array to function c 2b 2bhow to pass 2d array as function argument cpppassing two dimensional array to a function in c 2b 2b2d array in c 2b 2b functionhow to pass a 2d array to a function in c 2b 2bhow to get 2d array as a parameter in c 2b 2bpass a 2d array to a function c 2b 2bpassing 2d array in c 2b 2b in function