Solved

Bi dimensional table <> double pointer

Posted on 2004-10-14
9
248 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 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

12 Experts available now in Live!

Get 1:1 Help Now