Hi all,
how to pass the pointer of a two dimension array to a function?
Is it something like
Is the code void func( int[][] *array ) correct?Code:int array[10][5];
func( &array[0][0] );
...
...
void func( int[][] *array ) {
...
}
Printable View
Hi all,
how to pass the pointer of a two dimension array to a function?
Is it something like
Is the code void func( int[][] *array ) correct?Code:int array[10][5];
func( &array[0][0] );
...
...
void func( int[][] *array ) {
...
}
i think its right.....
You need to pass a pointer that contains the start address of the multidimensional array to the function, not the array itself.
I always thought 2D arrays and pointers to pointers were interchangable but I just tried it and it seems they're not.
you could pass that kind of pointer with a struct, also. :)
i.e., Array *arrayPtr...Code:struct array {
char *rows; //char stands for *type*
char *columns;
} Array
Here is something worth to knowQuote:
Originally Posted by samGwilliam
> int array[10][5];
> func( &array[0][0] );
> ...
> ...
> void func( int[][] *array )
Easy, just copy/paste the declarations
int array[10][5]; // this is your array
void func( array[10][5] ); // this is the prototype
The array definition is exactly the same.
func( array ); // this is the call, passing the array to the function
You use the same [x][y] indexing inside the function as you would outside the function.
I always pass 1D arrays as pointers though...Quote:
Originally Posted by Micko
Arrays are always passed as a pointer to the first element of the array, whether you say "int *" or "int []" in the prototype of the function.