Passing multidimensional array to a function returning multidimensional array from function

In this post we will see how to pass two dimensional array and three dimensional array to a function in C++.We will also see how to return two dimensional array and three dimensional array from a function.How to pass a and return a one dimensional array to and from a function is discuss in another post.

LinK: passing array to function and returning array from function

If you do not know how to declare a multi-dimensional array or if you totally have no idea about multi-dimensional array you can check the link given below.

Link: multidimensional arrays:2D , 3D and 10D array

Like the one dimensional array when passing a 2 dimensional array to a function we can use the name of the array.Consider the code below.

void func2DArray(int ar[][5] ,size_t sz )
{
//do anything with 'ar' here
}

void func2DArray1(int (*pt)[5] ,size_t sz ) //workfine
{
//do anything with 'pt' here
}

int main( )
{
int arr[3][5]={0} ;

func2DArray( arr , 3 ) ;

func2DArray1(arr , 3 ) ;

return 0;
}

when ‘func2DArray’and ‘func2DArray1’ is called the second argument passed is the number of rows and this is necessary.If you look at both the function definition we cannot determine the number of rows of the array passed.And this is dangerous! we may access a storage which may not belong to the array and this can lead to undefined result.


Returning 2 dimensional array from a function

If you are returning a two dimensional array from a function the format of writing the function name changes completely.The format is shown below.

 
return_type (* function_name (parameter_list)) [number_of_columns]
 

I am sure it is confusing so let look at the code example below.

/*** look at the function name ***/
int (*func( int ar[][5] , size_t sz ) ) [5]
{
//do anything with 'ar'

return ar;
}


/*** Look at the function name ***/
int *( funcArr(int (*pt)[5] , size_t sz) ) [5]
{
//do anything with 'pt'

return pt ;
}

int main( )
{
int arr[3][5]={0};

//do not get confuse 'ptMulArr' receives the array returned by the function
int (*ptMulArr)[5]=func(ar , 3 );

 
int (*ptMulArr1)[5]=funcArr( ar , 3 );

return 0;
}

Even if the parameter is a pointer in ‘funcArr’ when writing the function name the new method is adopted.

Side note::in the section “Passing 2 dimensional array to function” in the code example the new format “return_type (* function_name (parameter_list)) [number_of_columns]” is not used because the return type is a void type.

There is another way to return multidimensional array from the function.It is discussed in the next section.


Returning 2D array from a function using decltype() function

We can also use the decltype() function to return a 2 dimensional array from a function.Note the argument supplied to ‘decltype()’ must be a pointer to 2 dimensional array and the number of columns must also match with the number of array’s column that is to be returned.

If decltype() is used the format of writing the function follows the normal format.

int (*arR)[5];
int (*pt)[4] ;

decltype(arR) func(int ar[3][5])
{
//do anything with ar here

return ar ;
}


decltype(pt) func(int ar[3][5]) /** error!!! ***/
{
//do anything with ar here

return ar ;
}

int main( )
{
int arr[3][5]={0};

int (*ptAr)=func(ar) ;

return 0;
}

For the second function you will get an error because the number of columns(5) of the array returned by the function does not match with the number of columns(4) of the pointer to 2D array from which the type is deduce.So be careful to match the number of columns while using decltype() function to deduce the type.


Passing three dimensional array to a function

If you are very clear with passing 2 dimensional array to a function ,then passing three dimensional array or four dimensional or even 100 dimensional array becomes easy.They follow the trend of the two dimensional array.Let’s look at the code below where we will pass 3D array to a function.

void func(int ar[][4][5] , size_t sz )
{
//do anything with ar
}

void funcAr( int (*pt)[4][5] , size_t sz )
{
//do anything with ar here
}

int main( )
{
int arr[3][4][5]={0} ;

func(arr , 3) ;

funcAr(arr , 3) ;

return 0;
}

Well the concept is similar to 2 dimensional array so not much to say here.


Returning 3d array

To return a 3 dimensional array you can either use the new format mentioned in 2 dimensional array section but with extra square bracket after the last square bracket or you can use the decltype() function .An example is shown below for both of these methods.


int (*a)[4][5];

/*** Returning 3D array using the new method ***/
int *( func( int *ar[3][4][5] ) ) [4][5] 
{
//do anything with ar

return ar;
}

/*** Return 3d array with decltype() **/
decltype(a) func3DArr( int (*pt)[4][5] , size_t sz)
{
//do anything with ar

return ar ;
}

int main( )
{
int Ar3D[3][4][5]={ 0 } ;

int (*pt)[4][5]=func( Ar3D ) ;

int (*PtAr)[4][5]=func3DArr(Ar3D , 3 ) ;

return 0 ;
}

Note when using ‘decltype( )’ the argument supplied must have the number of second and third index value same as the second and third index value of the array returned by the function else you will get an error.

If you are returning 100D array then add 99 subscript to the function name or if you are using decltype() the number of indexes from 2nd to 99th must match.



Leave a Reply

Your email address will not be published. Required fields are marked *