Re: Two dimensional pointers and Two dimensional arrays

J

John Harrison

Vivek said:
Hello,
I know this is a pretty old topic, and must have been discussed
time and again. I want to the exact reason why a two d array(ex. int
arr[20][10]) can't be accessed using a two d pointer(int **p). In the
posts which I have gone thru, state that the two d array, arr, is an
array of arrays. So, what arr actually points to is the first element
of the 2 d array, this element being an array of 10 integers.
The thing which is not clear is: Consider int b[10], and int * q.
Here b is an array of 10 integers,and can be accessed using the
pointer q.

Simple

int* p;
int a[10];

Each element of a is an int, p points to an int. SAME.

int** p2;
int a2[10][10];

Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.

That's the reason.

john
 
K

Kevin Goodsell

John said:
Simple

int* p;
int a[10];

Each element of a is an int, p points to an int. SAME.

int** p2;
int a2[10][10];

Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.

That's the reason.

That was a great explanation.

Here's another way of looking at it. p2 is a "pointer to pointer to
int". Therefore, it can point to a "pointer to int". Where exactly is
this "pointer to int" to which p2 should point? There certainly is no
such thing in a2.

-Kevin
 
D

David White

Vivek said:
"John Harrison" <[email protected]> wrote in message
Simple

int* p;
int a[10];

Each element of a is an int, p points to an int. SAME.

int** p2;
int a2[10][10];

Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.

That's the reason.

john

If 'int[10]' can be equivalent to 'int *'(1st case), why can't each
element of a2, which is again int[10] be equivalent to int *, in that
way a2 would be the same as int **, since a2 is a pointer to int[],
anyway.

An int * contains the address of an int. An array of int contains no
addresses. It consists of nothing but a sequence of int values. By assigning
an int * to the address of the first element of an array of int, you have
enough information to use the pointer as though it were an array. That is,
you can use the pointer to access every int in the array. But an array of
int is still not an int *.

DW
 
J

John Harrison

Vivek said:
"John Harrison" <[email protected]> wrote in message
Simple

int* p;
int a[10];

Each element of a is an int, p points to an int. SAME.

int** p2;
int a2[10][10];

Each element of a2 is an int[10], p2 points to an int*. DIFFERENT.

That's the reason.

john

If 'int[10]' can be equivalent to 'int *'(1st case), why can't each
element of a2, which is again int[10] be equivalent to int *, in that
way a2 would be the same as int **, since a2 is a pointer to int[],
anyway.

It could be, but it isn't because C wasn't designed that way.

What you asking for is that multidimensional arrays be stored as an array of
pointers, but they aren't.

john
 
D

Default User

Vivek said:
If 'int[10]' can be equivalent to 'int *'(1st case),


They are NOT equivalent. In certain circumstances (well most) the name
of an array is converted to a pointer to the first element. In other,
very important, circumstances this conversion is not done.
why can't each
element of a2, which is again int[10] be equivalent to int *, in that
way a2 would be the same as int **, since a2 is a pointer to int[],
anyway.

C++, like C, does not support two-dimensional arrays. What you really
have are arrays of arrays. As such, a pointer to pointer can't work
because the compiler doesn't know the stride, i.e. how much to move
within the assigned memory for element access.

Think about it, you'd have the same **p for any two-dimensional array,
regardless of the element size. The compiler knows how to move within
the memory by the type of element pointed to. I wish I was one of these
guys that can make cool ASCII art to illustrate, but I'm not.




Brian Rodenborn
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top