Double pointer and 2D array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ur8x@ur8x.com

    Double pointer and 2D array


    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.
  • Richard Pennington

    #2
    Re: Double pointer and 2D array

    ur8x@ur8x.com wrote:
    [color=blue]
    > 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.[/color]

    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: rich@pennware.c om
    http://www.pennware.com ftp://ftp.pennware.com

    Comment

    • Joe Wright

      #3
      Re: Double pointer and 2D array

      ur8x@ur8x.com wrote:[color=blue]
      > 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.[/color]

      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:joewwrig ht@comcast.net
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • ur8x@ur8x.com

        #4
        Re: Double pointer and 2D array

        Joe Wright <joewwright@com cast.net> wrote:[color=blue]
        > 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.[/color]

        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]'?

        Comment

        • Richard Pennington

          #5
          Re: Double pointer and 2D array

          ur8x@ur8x.com wrote:
          [color=blue]
          > Joe Wright <joewwright@com cast.net> wrote:
          >[color=green]
          >>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.[/color]
          >
          >
          > 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]'?
          >[/color]

          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: rich@pennware.c om
          http://www.pennware.com ftp://ftp.pennware.com

          Comment

          • ur8x@ur8x.com

            #6
            Re: Double pointer and 2D array

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

            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.

            Comment

            Working...