Solved

Bi dimensional table <> double pointer

Posted on 2004-10-14
9
249 Views
Last Modified: 2010-04-15
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 !
0
Comment
Question by:matthew016
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12306608
change the function signature to:

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

Author Comment

by:matthew016
ID: 12306649
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
 
LVL 11

Accepted Solution

by:
cjjclifford earned 250 total points
ID: 12306689
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
 
LVL 9

Author Comment

by:matthew016
ID: 12306725
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 5

Expert Comment

by:van_dy
ID: 12306780
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12306785
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
 
LVL 5

Expert Comment

by:van_dy
ID: 12306790
LOL, seems like it got answered b4 i clicked the submit button.
0
 
LVL 9

Author Comment

by:matthew016
ID: 12306806
Oki Thanks, i get it ;-)
(sorry van_dy lol)
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12306901
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

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now