• C

# 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 !
LVL 9
###### Who is Participating?

Commented:
its to do with sizing/alignments as far as I remember, a 2 dimensional array in C is actually an array of arrays, so [4][5] becomes [ [5], [5], [5], [5] ] (or is it the other way arround.....) so, with int **ip, ip++ cannot be calculated correctly unless the size of the internal array is known...
0

Commented:
change the function signature to:

void transpose( int[][], int[][] );
0

Author Commented:
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)  {
0

Author Commented:
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 ? :-)
0

Commented:
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
0

Commented:
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....
0

Commented:
LOL, seems like it got answered b4 i clicked the submit button.
0

Author Commented:
Oki Thanks, i get it ;-)
(sorry van_dy lol)
0

Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.