ur**@ur8x.com wrote:
Joe Wright <jo********@comcast.net> wrote:
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
--
Richard Pennington
Email:
ri**@pennware.com http://www.pennware.com ftp://ftp.pennware.com