Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

Bi dimensional table <> double pointer

Hello,
I can't understand why this not works,

int tab1[15][20];
int tab2[20][15];
transpose(tab1,tab2);

void transpose (int **tab1, int **tab2) {
/*       cannot convert parameters from 'int [15][20]' to 'int ** '        */
...
}

Because when I try to get an int of my two dimension table with a double pointer it works,

int tab[4][5] = {5,10};

printf("%d\n", **tab);
/* 5 */
printf("%d\n", *(*tab+1));
/* 10 */

If someone know why this, help !
Avatar of cjjclifford
cjjclifford

change the function signature to:

void transpose( int[][], int[][] );
Avatar of matthew016

ASKER

I know i can use this
void transpose (int tab1[15][20], int tab2[20][15])  {
or this
void transpose (int tab1[][20], int tab2[][15])  {
or this
void transpose (int (*tab1)[20], int (*tab2)[15])  {

but I don't understand why I can't use this one

void transpose (int **tab1, int **tab2)  {
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, so once I put a signature with the number of columns
void transpose (int ptr1[][20], int ptr2[][15])  {

I can use
ptr2++
and
*(*ptr2+1)
*(*(ptr2+1))

Because now it knows how it has to be calculated because
 he knows the constant size of the internal arrays
I am right ? :-)
Hi,
the more general rule is that if a multidimensional array has to
be passed to a funtion, only the first dimension is free, rest of the dimensions
will have to be specified.

for example, consider a 2 *  3 array, this is how the elemnts are stored in memory,
[0,0] [0,1] [0,2][1,0][1,1][1,2]  <---- array A;

if u pass to function as::
void transpose(p)
               int **p;
{
   ..........

Imagine referencing A[1][2];   It wont be possible to reference this element through p
unless it is known that  A[1][0]  comes after A[0][2]. So u need to tell the second index
size.

hope this helps,
van_dy
well.... take care with this, as the number of columns of one is the number of rows of the other :-)

what you may want to do is also pass in the number of rows/columns expected, to ensure this is a valid call....
LOL, seems like it got answered b4 i clicked the submit button.
Oki Thanks, i get it ;-)
(sorry van_dy lol)
Hi matthew016,
Two-dimensional array constants are "flat" arrays, whereas double-pointers are ... double pointers.

Or, a bit more technical:

if you have
int a[3][5];
int b[15];

&a[2][2]-&a
is the same as
&b[2*3+2]-&b


Cheers!

Stefan