How to pass 2D array to a function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to pass 2D array to a function

i want to calculate the addition of 2 2D matrix in function called by main and return type must b 2D array

23rd Jun 2016, 7:53 PM
Zuber Ahmed
Zuber Ahmed - avatar
4 Answers
+ 1
you need not return a 2d array work like this.... void use2darrayfunction ( int arr [][***] ) in place of *** you have to put some numerical constant.... for example if your array was declared like this in main () int arr [10][25]; then call the function like this use2darrayfunction ( arr ); and the value in the place of *** should be 25 use the array as you want and the original array will change from the function itself....
25th Jun 2016, 2:09 PM
Mukul Kumar
Mukul Kumar - avatar
+ 1
if you want the array to be treated like an array (without losing the size property) you should do( for a a 2d array of ints ): template <int N, int M> void my_function(int (&arr)[N][M]) { ... } n and m are the sizes of the array if you dont care about having a pointer, call it like this: void muy_function(int **arr) { ... }
25th Jun 2016, 4:23 PM
Garme Kain
Garme Kain - avatar
0
An example: void myfunc(int arr2d[5][30]) Returning an int** works in other compilers...
23rd Jun 2016, 9:26 PM
Stefan
Stefan - avatar
0
dude....thats wrong....
25th Jun 2016, 2:06 PM
Mukul Kumar
Mukul Kumar - avatar