Passing and returning array and multidimensional array from a function


Passing and returning array and multidimensional array from a function

In this post we will discuss how to pass an array to a function ,we will also discuss how to pass two dimensional array and three dimensional array to a function.We will also see how to return an array from a function ,and also return a two dimensional array and three dimensional array from a function.But ,before starting the discussion first of all if you don’t know how to declare a multidimensional array then please learn it.

Link:Multidimensional array

Passing an array to a function

Passing an array to a function is quite simple.To pass an array we will only pass the name of the array say for example consider the array arr[] given below.

int arr[]={3,4,5,5} ;

To pass ‘arr[]’ array to any function we will pass only the name of the array,in this case ‘arr’ will be passed.Consider the code below.

void func( int *i) ///receive an array, as a pointer
{
cout<< i[2] ;
}

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

func(arr) ;///passing the array

return 0 ;
}

In the function definition above we receive the array as a pointer.This is acceptable because whenever we pass an array we are passing the pointer of an array.

There is another method to accept an array in the function.In this method we will receive the array using the array syntax,meaning using the square bracket syntax.When using this method you can either mentioned the index value or you may not ,in both the cases it works fine.Consider the code below.

void func( int i[]) /*receive array using the square([]) bracket syntax without index value*/
{
cout<< i[2] ;
}

void funcNew( int i[4] ) ///receive array using the square([]) bracket syntax with index value
{
cout<< i[2] ;
}

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

func(arr) ;///passing array

return 0 ;
}

Whenever you pass an array and the function receives it as a pointer or receives it using the square bracket syntax but without the index value,then it is sensible to pass the size of the array also.If the array size is not passed you cannot know how many elements the array hold so you may try to access the storage way beyond the limited size.For instance,consider the code below.

void func( int i[]) ///receive array ausing the square([]) bracket syntax
{
for(int num=0; i<6 ;num++) //the size of array is not known exactly(dangerous)
{ cout<< i[num] }
}

/***
Solution
****/

void func( int i[] ,size_t sz) ///receive array using the square([]) bracket syntax
{
for(int num=0; i<sz ;num++) //No problem now
{
 i[num]=i*i ;
}
}

If however you used the array syntax and the index value is mentioned,then you can easily get the size of the array using the operation ‘sizeof(array_name)/sizeof(array_type)‘.An example of this implementation is shown below.

void func(int i[4])
{
for(int num=0 , size=sizeof(i)/sizeof(int); num<size ; num++)
{ cout<< i[num] ;}
}

Note here getting the size using the operation ‘sizeof(array_name)/sizeof(type)’ works only if the index value is mentioned.Without the index value you will always get the result as 1.Without the index value the array name is just a normal pointer and the size of a pointer is 4 and the size of int type is 4 so 4/4,we get 1.

Now that we know how to pass an array to a function let us see how to return an array from a function.





Returning array from a function

To return an array from a function the most important thing to be concerned about is the return type.For a simple array making the return type as a pointer will do the trick.In the receiving side the type must be also a pointer.An example code returning an array from a function is shown below.

int *func( int *ar , size_t sz )
{
//do anything with ar here

return ar ;
}

int main()
{
int ar[]={4,5,5,5}

int *arPt=func( ar , (sizeof(ar)/sizeof(int)) ); /* arPt pointer receives the array returned by func() */

cout<< arPt[3] ;

return 0 ;
}

That was easy,isn’t?

In the function above we have given the return type as a pointer to int type explicitly.There is another way to make the compiler deduce the return type for us automatically.Here we will be using a function known as decltype().This function is added in the new C++ standard known as C++11.This function will accept a pointer to an array as an argument but it must be of the same type as the return array type.Consider the code below.

Link:C++11 using decltype()

int *arr ; //a pointer to an array

decltype(arr) func( int ar[] , size_t sz ) ///work fine
{
//do anything with ar here

return ar ;
}

int main()
{
int ar[]={3,34,4,5,5}

int *ip=func(ar,sizeof(ar)/sizeof(int)) ;

return 0;
}

The decltype() deduce that the return type is a pointer to int type from the ‘arr’ pointer supplied as the argument.So the above way of specifying the type is same as the previous function where pointer to int type is used as a return type.Here we are only allowing the compiler to deduce it for us.

There is all to it about passing an array to a function and returning array from a function.Next let us see how to pass multidimensional array namely 2D and 3D array.


 


Stop wasting time,earn money($$$) from your website-Join Now!

Passing 2 dimensional array to function

I am guessing you already know how to write a two dimensional array and also the format of the pointer syntax that can point to it.If you have forgotten I am going to remind you in brief here.Look at the code given below.

int arMul[3][5];///a two dimensional array example

int (*arPtMul)[5] ; /*arPtMul is a pointer that can point to 2 dimensional array
having the number of columns as 5*/

arPtMul=arMUl ;///work fine

Note carefully the pointer syntax,it can be confusing with *[] format which has a different meaning.Since that now we know how to write a 2 dimensional array and the syntax of the pointer that can point to it you will see that passing a two dimensional array to a function will become pretty easy.

Like the one dimensional array when passing a 2 dimensional array we only use the name of the array.And also when receiving it in the function we can use the two dimensional array syntax or the pointer format.Consider the code below.

void func2DArray(int ar[][5] ,size_t sz ) ///work fine
{
//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;
}

Here it is sensible to pass the number of rows if the function parameter receive the array passed without the number of rows for the same reason mentioned in one dimensional array;to prevent unintentional accessing of storage beyond the limited size.


 


Stop wasting time,earn money($$$) from your website-Join Now!

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 us look at the code example given below.

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

return ar;
}

int *( funcArr(int (*pt)[5] , size_t sz) ) [5] {
//do anything with ar

return pt ;
}

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

int (*ptMulArr)[5]=func(ar , 3 );

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

return 0;
}

Even if the parameter is a pointer to two dimensional array in 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.


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.The argument supplied to decltype() must be a pointer to 2 dimensional array and the number of columns must also match with the return array’s columns.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 number of columns does not match
{
//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 given below.

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.

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];

int *( func( int *ar[3][4][5] ) ) [4][5] ///work fine
{
//do anything with ar

return ar;
}

decltype(a) func3DArr( int (*pt)[4][5] , size_t sz) ///work fine
{
//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.


Conclusion

Passing an array to a function and returning an array from a function is of no use if you are using class or structure:user-defind data type.In using these type since the array is part of the class as a data member you can easily access the array without passing them.Moreover,the need for returning an array from a function is avoided which indeed prevents you from putting up with various complex function declaration format.All these advantages of using class also holds true for multidimensional array;all the more reason to use class or structure if you are using say a 100 dimensional array(just an idea,do not try it) in your program.


Related Link

->C and C++ Using array :myths and conception.

->C++ Function:Declaration and definition.

->Relation between array and pointers.