Solved

Bi dimensional table <> double pointer

Posted on 2004-10-14
9
250 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

785 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