Re: Two dimensional pointers and Two dimensional arrays

Discussion in 'C++' started by John Harrison, Aug 18, 2003.

  1. "Vivek" <> wrote in message
    news:...
    > 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
     
    John Harrison, Aug 18, 2003
    #1
    1. Advertising

  2. John Harrison wrote:

    >
    > 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
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 18, 2003
    #2
    1. Advertising

  3. John Harrison

    David White Guest

    Vivek <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<bhq2sh$1se1v$-berlin.de>...
    > > 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
     
    David White, Aug 19, 2003
    #3
  4. "Vivek" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<bhq2sh$1se1v$-berlin.de>...
    > > 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
     
    John Harrison, Aug 19, 2003
    #4
  5. John Harrison

    Default User Guest

    Vivek wrote:

    > 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
     
    Default User, Aug 19, 2003
    #5
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    454
    Alf P. Steinbach
    Aug 18, 2003
  2. Icosahedron
    Replies:
    8
    Views:
    687
    Vivek
    Aug 21, 2003
  3. Richard Hayden

    C multi-dimensional arrays and pointers

    Richard Hayden, Nov 1, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,040
    Dave Thompson
    Nov 8, 2004
  4. Kev Jackson
    Replies:
    2
    Views:
    141
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    228
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page