Double pointer and 2D array

Discussion in 'C Programming' started by ur8x@ur8x.com, Aug 22, 2004.

  1. Guest

    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.
    , Aug 22, 2004
    #1
    1. Advertising

  2. wrote:

    > 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

    --
    Richard Pennington
    Email:
    http://www.pennware.com ftp://ftp.pennware.com
    Richard Pennington, Aug 22, 2004
    #2
    1. Advertising

  3. Joe Wright Guest

    wrote:
    > 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.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 22, 2004
    #3
  4. Guest

    Joe Wright <> 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]'?
    , Aug 22, 2004
    #4
  5. wrote:

    > Joe Wright <> 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:
    http://www.pennware.com ftp://ftp.pennware.com
    Richard Pennington, Aug 22, 2004
    #5
  6. Guest

    Richard Pennington <> wrote:
    > 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.
    , Aug 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,444
    Victor Bazarov
    Feb 17, 2005
  2. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    337
    Tim Rentsch
    Oct 9, 2004
  3. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    663
    Martin Ambuhl
    Jan 28, 2008
  4. Replies:
    5
    Views:
    416
    James Kanze
    Jun 27, 2008
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    439
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page