Double pointer and 2D array

U

ur8x

I have a double pointer and a 2D array:

int mat[5][5], **ptr;
ptr = mat;

mat[2][3] = 3;

Although mat and ptr are pointing at the same address,
&ptr[2][3] and &mat[2][3] are not, why?

I know ptr[2][3] dereferences the resulting object two
full time which will have the address of whatever content
that is at [2][3] rather than the address TO [2][3] but I
can't figure out why?

Thank you.
 
R

Richard Pennington

I have a double pointer and a 2D array:

int mat[5][5], **ptr;
ptr = mat;

mat[2][3] = 3;

Although mat and ptr are pointing at the same address,
&ptr[2][3] and &mat[2][3] are not, why?

I know ptr[2][3] dereferences the resulting object two
full time which will have the address of whatever content
that is at [2][3] rather than the address TO [2][3] but I
can't figure out why?

Thank you.

Hi,

You need to declare ptr as "a pointer to an array of 5 integers":

int (*ptr)[5];

You should have gotten a warning from the compiler about your
ptr = mat;
statement. Warnings are our friend!

The compiler needs to know, for a multi-demensional array, how
to index into it. The 5 in the pointer declaration above is the
size of the last array in mot.

If mot had 3 dimensions, e.g.:

int mot[3][4][5];

Then ptr would have to look like:

int (*ptr)[4][5];

It turns out that the compiler doesn't need the first dimension
to index into an array.

-Rich
 
J

Joe Wright

I have a double pointer and a 2D array:

int mat[5][5], **ptr;
ptr = mat;

mat[2][3] = 3;

Although mat and ptr are pointing at the same address,
&ptr[2][3] and &mat[2][3] are not, why?

I know ptr[2][3] dereferences the resulting object two
full time which will have the address of whatever content
that is at [2][3] rather than the address TO [2][3] but I
can't figure out why?

Thank you.

First, 'int mat[5][5]' is simply 25 ints. A pointer to it would be
'int (*ptr)[5] = mat;'. Now ptr[0][0] is the first of them and
ptr[4][4] is the last of them.
 
U

ur8x

Joe Wright said:
First, 'int mat[5][5]' is simply 25 ints. A pointer to it would be
'int (*ptr)[5] = mat;'. Now ptr[0][0] is the first of them and
ptr[4][4] is the last of them.

Ok, my question is, assuming my first post, that is ptr = mat, why
ptr[0][0] holds an address (I know it does not get the intended
answer, I want to know why).

P.S. Is there a difference between `int * ptr[5]' and `int (*ptr)[5]'?
 
R

Richard Pennington

Joe Wright said:
First, 'int mat[5][5]' is simply 25 ints. A pointer to it would be
'int (*ptr)[5] = mat;'. Now ptr[0][0] is the first of them and
ptr[4][4] is the last of them.


Ok, my question is, assuming my first post, that is ptr = mat, why
ptr[0][0] holds an address (I know it does not get the intended
answer, I want to know why).

P.S. Is there a difference between `int * ptr[5]' and `int (*ptr)[5]'?

int **ptr;

"ptr is a pointer to a pointer to int."

In the following, the offsets are in bytes (no pointer arithmetic)
and sizeof(int) is assumed to be 4.

ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4))

int (*ptr)[5];

"ptr is a pointer to an array of 5 int."

ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4 * 5))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4 * 5))

Where the 5 above is the number of array elements in the second
dimension.

Quite different addresses!

int *ptr[5];

"ptr is an array of 5 pointers to int."

Note that the precedence of [] is higher than *.

-Rich
 
U

ur8x

Richard Pennington said:
int **ptr;
"ptr is a pointer to a pointer to int."
In the following, the offsets are in bytes (no pointer arithmetic)
and sizeof(int) is assumed to be 4.
ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4))
int (*ptr)[5];
"ptr is a pointer to an array of 5 int."
ptr[0][0] generates code like *((*p + (0 * 4)) + (0 * 4 * 5))
ptr[1][1] generates code like *((*p + (1 * 4)) + (1 * 4 * 5))
Where the 5 above is the number of array elements in the second
dimension.
Quite different addresses!
int *ptr[5];
"ptr is an array of 5 pointers to int."
Note that the precedence of [] is higher than *.

Excellent, now it makes sense. A double pointer just simply
does not skip the width (or the column) of the array since
it's, well a double pointer, not a pointer to the array of
such dimension.

Thank you for your help.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top