pointers and array

A

Anthony Moss

How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;

-
-
-
-
-

etc.
thanks
Anthony
 
A

Al Bowers

Anthony said:
How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;

int (*p)[3];

Example:
#include <stdio.h>

int main(void)
{
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int (*p)[3];

p = a;
printf("a[1][1] = %d\np[1][1] = %d\n",
a[1][1],p[1][1]);
return 0;
}
 
R

Richard Bos

Anthony Moss said:
How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;

You don't. If you want to work with the sub-arrays of a (which is rare),
you need a pointer to an array: int (*p)[3]. If, OTOH, you want to work
with the individual int members, you need a simple int *.
The declaration int ** p means that p is a pointer _to a pointer_ to
int. You have no pointers to int to point at, so you should not be using
an int **.

Richard
 
G

Guillaume

How do you use a double pointer to access a two dimensional array.
ie int a[3][3];
int **p;

Be careful not to confuse:

int a[][]

with:

int * a[]

These are completely different beasts.

While with:

int a[m][n]

a[j] is the same as: *(a + n*i + j)

with:

int * a[n]

a[j] is the same as: *(*(a + i) + j)
 
D

David Rubin

Dan said:
How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;


You don't. You may want to read the FAQ, BTW.

Dan

Specifically question 6.2. I was just helping someone with this last night, and
it is very confusing.

int a[1][1] = {{0}};
int (*p)[1] = a;
int **q = (int **)a;

printf("p=%p p[0]=%p\n", (void *)p, (void *)p[0]);
printf("q=%p q[0]=%p\n", (void *)q, (void *)q[0]);

produces (for example)

p=0x80605412 p[0]=0x80605412
q=0x80605412 q[0]=(nil)

So, even though p and q have the same value, p[0] and q[0] do not. This seems to
be entirely due to the fact that p[0] is array-of-int while q[0] is
pointer-to-int. p and p[0] have the same value for the same reason that &a and a
have the same value. The fact that the treatment and interpretation of p[0] and
q[0] are different is just an artifact of the type system.

/david
 
D

David Rubin

Guillaume said:
While with:

int a[m][n]

a[j] is the same as: *(a + n*i + j)

with:

int * a[n]

a[j] is the same as: *(*(a + i) + j)


That is a rather nice summary!

/david
 
C

Chris Torek

[some editing for space below]
Guillaume said:
While with: int a[m][n]
a[j] is the same as: *(a + n*i + j)

with: int * a[n]
a[j] is the same as: *(*(a + i) + j)


That is a rather nice summary!

Yes, except for one technical flaw -- the first expression is not
legal C -- and one more serious one: the second expression works
for the array of arrays case as well.

I have a pictorial explanation of what a pointer-to-array "means"
on my web pages; at some point I should add an "array of pointers"
to contrast with "array of arrays" and "pointer to array".
 
J

Joona I Palaste

Guillaume said:
While with: int a[m][n]
a[j] is the same as: *(a + n*i + j)

with: int * a[n]
a[j] is the same as: *(*(a + i) + j)
That is a rather nice summary!


Yes, except for one technical flaw -- the first expression is not
legal C

How is it not legal, please?

Well, the expressions aren't equivalent. a[j] has type "int" but
*(a + n*i + j) has type "int (*)[n]". It's legal C, all right - but
if the value of i is too high, you end up reading unallocated memory,
because *(a + n*i + j) is the same as a[n*i + j].
 
J

Joona I Palaste

Joona I Palaste said:
Guillaume said:
While with: int a[m][n]
a[j] is the same as: *(a + n*i + j)

with: int * a[n]
a[j] is the same as: *(*(a + i) + j)
That is a rather nice summary!

Yes, except for one technical flaw -- the first expression is not
legal C

How is it not legal, please?

Well, the expressions aren't equivalent. a[j] has type "int" but
*(a + n*i + j) has type "int (*)[n]". It's legal C, all right - but
if the value of i is too high, you end up reading unallocated memory,
because *(a + n*i + j) is the same as a[n*i + j].


What am I babbling about? *(a + n*i + j) has type "int[n]" which decays
into type "int *" when used in a value context. a itself has type
"int[m][n]" which decays into "int (*)[n]" when used in a value context.
I must be too drunk. =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top