Thread: Multidimensional Arrays

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Multidimensional Arrays

    Okay let's say that we have an 3x3 array of int*.

    Now since the array (a 3x3 array of int*) needs to be dereferenced 3 times (***) to get the value of an int that one of the elements hold the address to, this must mean that just having the array name point to the first element of row one can't be possible because then only 2 levels of indirection would be needed to gain access to an int value. This must mean (for this individual example, a 3x3 int* array), that 3 additional pointers will be needed. These additional pointers (to pointers, int**) will hold the address of the first pointers in each row of the array. The actual array name will hold the address of the first of the previous 3 pointers (this pointer, an int***) and each of the 3 previous pointers being next to each other in memory (so that pointer arithmetic can take place).

    Now this is just how I see it as working. Is this even correct? (If not I would like to know how this actually works..)

    Now if this is correct, I already see that the same thing would be needed for a simple 2D array of int (rather than int*), except step each pointer level down 1 notch..
    Last edited by homeyg; 03-06-2006 at 07:59 PM.

  2. #2
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    yes for a three level array this would be correct, but a 3x3 array usually means three rows by three colums, not three directions of space.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by loopshot
    yes for a three level array this would be correct, but a 3x3 array usually means three rows by three colums, not three directions of space.
    Thats what I meant, a 2D array of int*, 3 rows of 3 columns.

    Would that make it not correct?
    Last edited by homeyg; 03-06-2006 at 08:31 PM.

  4. #4
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    yea because in this array you only need to specify the row, then the colum to access a element, think about it.

    array[0][0], first element
    array[2][2], last element
    array[1][1], middle element

    as you can see we can get every where with 2 specifications

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by loopshot
    yea because in this array you only need to specify the row, then the colum to access a element, think about it.

    array[0][0], first element
    array[2][2], last element
    array[1][1], middle element

    as you can see we can get every where with 2 specifications
    I think you've misunderstood; To get the int value held in the address in say, array[0][0] (since this is a 2D array of pointers to int), you need to dereference it 1 more time - *array[0][0];

    And the first post was my idea of how this is worked out..
    Last edited by homeyg; 03-06-2006 at 08:43 PM.

  6. #6
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    yea but it is still the same thing, you only need 2 pointers to access any data in the array, then you can dereference or what every, it dosent matter.

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by loopshot
    yea but it is still the same thing, you only need 2 pointers to access any data in the array, then you can dereference or what every, it dosent matter.
    I'm not talking about how many times the thing has to be dereferenced to get whatever is stored in an element, I'm talking about how many hidden pointers are needed to make it work correctly. It is not possible for the array name to be a pointer to each array in the 2D array and a pointer to each element in those array so there has to be hidden pointers involved! That's what I'm asking about. I understand that you only have to dereference the thing twice (which is something I've always understood), but that is not what I'm asking about.
    Last edited by homeyg; 03-07-2006 at 08:53 AM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your original post was a bit hard to follow, but I think your understanding is correct.

  9. #9
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    i think we just had a misunderstanding about what you where talking about, i still dont.

  10. #10
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Sorry about the misunderstandings.

  11. #11
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Es refresque hombre, puesto preocupación sobre lo.
    Ha now i make you learn spanish to understand me.
    Hay if have to learn it is, am sure as hell am going to use it!!!

  12. #12
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by loopshot
    Es refresque hombre, puesto preocupación sobre lo.
    Ha now i make you learn spanish to understand me.
    Hay if have to learn it is, am sure as hell am going to use it!!!
    hmm worry about it something (used to take spanish)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM