Does anyone know how to pass an array of ints to a function?
i tried
and it didn't seem to work.Code:int a[2][2] = { {1, 2}, {3, 4} };
function(a);
void function(int **a) {
printf("%d\n", a[1][1]);
}
Any suggestions?
Printable View
Does anyone know how to pass an array of ints to a function?
i tried
and it didn't seem to work.Code:int a[2][2] = { {1, 2}, {3, 4} };
function(a);
void function(int **a) {
printf("%d\n", a[1][1]);
}
Any suggestions?
> void function(int **a) {
Should be one of these - choose which is most readable
void function(int a[2][2] ) {
void function(int a[][2] ) {
void function(int (*a)[2] ) {
You could also do:
void function(int a[][])
Right, Salem?
> void function(int a[][])
> Right, Salem?
No, not right.
You can only leave the leftmost [] empty.
I see. You can have:
But, doesn't that limit what you can do with something like this?Code:void function (int arr[]); /* legal */
void function (int arr[][2]); /* illegal */
--Garfield
Seems to work now.
Thanks
void function (int arr[][2]);
This is legal. Nothing wrong with it.
hi,
could anyone pls a complete sourcecode showing how to use the function in main? i tried several versions that didn't work. and how can i pass a pointer to the array, not just a copy of it?
> and how can i pass a pointer to the array, not just a copy of it?
Arrays are always passed as a pointer, you have no choice.
> could anyone pls a complete sourcecode showing how to use the function in main?
See the original poster
function(a);
is the way