+ 1

Passing multidimensional arrays [solved]

How do you pass a variable length multidimensional array to a function in C? Take the code snippet below as an example. Void foo(int **); main() { int a; scanf("%d",&a); int ar[a][a]; foo(ar); printf("%d",ar[0][0]); } Void foo(int **a) { a[0][0]=12; } in that snippet, I wanted to pass a VLA to the function foo which will then set element [0][0]. Then I wanted to print that change from main. But my function prototype is not correct. It will be easier if the second index was known before hand like ar[][5], but in this case, neither of the index is known at compile time as it depends on user input. I tried declaring the array as global but that was worse as it was declared before it's index. A quick solution would be to define the length of the array, but I want the user to enter the length of the array instead of tampering with the source code to change the defined value. So help!!

10th Jan 2021, 8:33 AM
Sekiro
Sekiro - avatar
6 Respuestas
+ 5
You can pass the variable length array to a function by first passing its dimensions: void foo(int m, int n, int a[m][n]); if you need, the first dimension can be omitted: void foo(int n, int a[][n]); Note that this approach only works in C, not in C++. https://code.sololearn.com/c3s1saJvYi13/?ref=app
10th Jan 2021, 9:41 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 4
There are many ways to represent a 2-dimensional array but the following example with a user-selected 1 dimensional array should get you started: #include <stdio.h> #include <stdlib.h> void foo(int *, int); int main() { int a; scanf("%d",&a); int *ar = (int*)malloc(a * sizeof(int)); foo(ar, a); printf("%d",ar[0]); // if this wasn't main, it would be important to call free(ar); // since this is main, free isn't needed as the memory will be freed automatically as the program closes. } void foo(int *a, int len) { a[0]=12; } I included a len parameter because if you were really looping through or processing all elements in the array, you'd need to know how many elements are in it. If you just want a square 2-dimensional array, I would actually just use a single block of memory like this example and you could translate any [x][y] indexes by doing something like x * size + y or x + y * size consistently. size would be the number of elements along a single dimension of the square array so 16 if it was supposed to be a 16 by 16 array. Another way to represent a 2-dimensional array would be an array of pointers to your allocated blocks of memory. This way, each block could be a different size to represent different array lengths. An array of pointers would leave you accessing each element with notations like [x][y].
10th Jan 2021, 9:00 AM
Josh Greig
Josh Greig - avatar
+ 1
Martin Taylor Thanks man. I appreciate your varied examples and the comments. Thanks
10th Jan 2021, 11:34 AM
Sekiro
Sekiro - avatar
0
Josh Greig Your example is pretty new to me. I didn't know you could traverse multidimensional arrays that way. I will read up on it. Thank you.
10th Jan 2021, 11:33 AM
Sekiro
Sekiro - avatar
0
Volodymyr Chelnokov Thanks man. That was to the point.
10th Jan 2021, 11:34 AM
Sekiro
Sekiro - avatar