I read a lot of stuff in here and tried many but i couldn't find a way to pass a multidimensional array to a function in C, change some of the values and somehow return the new array. It's important to find a way to pass that array further to another function and do the same thing.
I would like to find a way to pass the array to a function.Then pass it from the first function to a second one,do something there(maybe print,maybe change values),then use it again to the first function and finally use that array in main.
My last try is:
void func(int multarray[][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
I also tried this:
void func(int multarray[rows][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
I also tried this:
int
getid(int row, int x, int y) {
return (row*x+y);
}
void
printMatrix(int*arr, int row, int col) {
for(int x = 0; x < row ; x++) {
printf("\n");
for (int y = 0; y <col ; y++) {
printf("%d ",arr[getid(row, x,y)]);
}
}
}
main()
{
int arr[2][2] = {11,12,21,22};
int row = 2, col = 2;
printMatrix((int*)arr, row, col);
}
from here
I also tried double pointers.I also read that there is a different approach if the compiler does not support VLAs. I am using gnu.
Not exactly sure, what the problem is but this works (and prints the value "9"):
Notice, that the array decays to a pointer when passed to a function.
Several things to remember:
When you pass an array expression as an argument to a function, it will be converted from an expression of type "N-element array of
T
" to "pointer toT
", and the value of the expression will be the address of the first element of the array. The called function receives a pointer value.The
[]
operator can be used with expressions of array or pointer type; IOW, given the declarationsint a[10]; int *p = a;
, thenp[i]
anda[i]
refer to the same element.When declaring a function that accepts a VLA as a parameter, you must declare the parameters that specify dimension before you declare the array.
So, for a function that manipulates a 2D VLA, you'd write something like
What's up with
int (*multiarray)[cols]
? Remember that in passing the array expression as an argument, the type of the array expression is converted from "N-element array ofT
" to "pointer toT
". In this case,T
is "cols
-element array ofint
", so we're going from "rows
-element array ofcols
-element aray ofint
" to "pointer tocols
-element array ofint
". In the context of a function parameter declaration,T a[N]
,T a[]
, andT *a
are all identical; in all three cases,a
is declared as a pointer toT
. Soint (*multiarray)[cols]
is equivalent toint multiarray[][cols]
, which is equivalent toint multiarray[rows][cols]
. I prefer using the first form because it most accurately represents the situation.If you want to pass this array as an argument to another function, you'd use the same type:
Any changes to the array contents made in
foo
will be reflected inbar
andmain
.VLAs can be useful, but they have their limitations. They can't be declared
static
, nor can they be defined outside of a function. They cannot use{}
-style initialization syntax. Also, VLA support is now optional as of the 2011 standard, so you can't rely on them being supported everywhere.In the event you don't have VLAs available and your array size isn't known until runtime, you'll have to use dynamic memory allocation (
malloc
orcalloc
), and the types you pass to your functions will be different:One drawback with this approach is that the allocated memory isn't guaranteed to be contiguous (i.e., rows won't be adjacent in memory). If that matters, you'll have to go with yet another approach. Instead of allocating rows and columns separately, you allocate everything in one single block, and manually map array indices:
In this case, we've allocated a single buffer large enough for all elements, but we have to index it as a 1D array, computing the index as
i * rows + j
.For your 2 dimensional arrays I would define a type for it.
You can then declare variables of this type and pointers to them. This makes it easier to pass things around.
It is helpful to think of arrays as pointers to memory. Then it is easy to think of a 2d array as a pointer to a memory of pointers (kinda)
This is not going to work
but this worked just fine for me