I wanted to pass a multi-dimensional array to a function in C. As in, directly manipulate the contents of an array passed as a parameter.
After many hours of messing around with pointers and getting nowhere, I decided maybe I was trying to be too complicated and tried the following :
Sure enough, the result was :
myint works as I expected C to work. What I don't understand is why the multi-dimensional array is treated differently?
Thanks for any help you can give.
After many hours of messing around with pointers and getting nowhere, I decided maybe I was trying to be too complicated and tried the following :
Code: Select all
#include <stdio.h>
void changeArray(int testarray[2][2])
{
testarray[1][1]=3;
}
void changeInt(int test)
{
test=4;
}
int main()
{
int array[2][2] = { {1,1}, {1,1} };
int myint=1;
printf("%i %i\n",array[1][1],myint);
changeArray(array);
changeInt(myint);
printf("%i %i\n",array[1][1],myint);
}
Sure enough, the result was :
Code: Select all
$ ./PointerTest
1 1
3 1
$
myint works as I expected C to work. What I don't understand is why the multi-dimensional array is treated differently?
Thanks for any help you can give.