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 ) { ... }
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 ) { ... }
Last edited by SoFarAway; 04-12-2005 at 06:56 AM.
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.
When no one helps you out. Call google();
I always thought 2D arrays and pointers to pointers were interchangable but I just tried it and it seems they're not.
Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)
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 knowOriginally Posted by samGwilliam
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I always pass 1D arrays as pointers though...Originally Posted by Micko
Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.