References to multidimensional array

Discussion in 'C++' started by TLOlczyk, Oct 14, 2004.

  1. TLOlczyk

    TLOlczyk Guest

    I have a brain cramp and I need some help.

    I have a chunk of code below which demonstrates
    a problem I have with multidimensional arrays.
    I want to keep it simple but something specific is getting in the way.

    int a[10][10];
    int b[10][10];
    int **present;
    int **next;

    bool done=false;

    while(!done)
    {
    // Loop which substitutes for complex calculation
    for(int i=0;i<10;i++)
    for(int j=0;j<10;j++)
    next[j]=present[j];
    int **temp=next;
    next=present;
    present=temp;
    test_for_done(present);
    }

    Compare this to the one-dim version ( which works )
    int a[10];
    int b[10];
    int *present;
    int *next;

    bool done=false;

    while(!done)
    {
    // Loop which substitutes for complex calculation
    for(int i=0;i<10;i++)
    next=present;
    int *temp=next;
    next=present;
    present=temp;
    test_for_done(present);
    }




    The reply-to email address is .
    This is an address I ignore.
    To reply via email, remove 2002 and change yahoo to
    interaccess,

    **
    Thaddeus L. Olczyk, PhD

    There is a difference between
    *thinking* you know something,
    and *knowing* you know something.
    TLOlczyk, Oct 14, 2004
    #1
    1. Advertising

  2. TLOlczyk wrote:
    > I have a brain cramp and I need some help.
    >
    > I have a chunk of code below which demonstrates
    > a problem I have with multidimensional arrays.
    > I want to keep it simple but something specific is getting in the way.
    >
    > int a[10][10];
    > int b[10][10];
    > int **present;
    > int **next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > for(int j=0;j<10;j++)
    > next[j]=present[j];
    > int **temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }
    >
    > Compare this to the one-dim version ( which works )
    > int a[10];
    > int b[10];
    > int *present;
    > int *next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > next=present;
    > int *temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }


    The code you posted doesn't seem to use 'a' or 'b' in either variation.

    I suspect that the problem you have is due to the fact that an array
    of more than one dimension cannot be converted to a pointer to pointer.
    They are incompatible types.

    Perhaps we can come back to this when you post the actual code with which
    you have the problem.

    Victor
    Victor Bazarov, Oct 14, 2004
    #2
    1. Advertising

  3. TLOlczyk

    Howard Guest

    "TLOlczyk" <> wrote in message
    news:...
    >I have a brain cramp and I need some help.
    >
    > I have a chunk of code below which demonstrates
    > a problem I have with multidimensional arrays.
    > I want to keep it simple but something specific is getting in the way.
    >
    > int a[10][10];
    > int b[10][10];
    > int **present;
    > int **next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > for(int j=0;j<10;j++)
    > next[j]=present[j];


    At this point, you have undefined behavior, since neither next nor present
    have been initialized to anything! (Did you mean to initialize them to
    point to the start of a or b?)

    > int **temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }
    >
    > Compare this to the one-dim version ( which works )
    > int a[10];
    > int b[10];
    > int *present;
    > int *next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > next=present;


    Same problem here. THis causes undefined behavior.

    > int *temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }
    >


    Perhaps if you explained what you're trying to do?

    -Howard
    Howard, Oct 14, 2004
    #3
  4. hi,

    that is going to give compile errors most likely, and it *definitely*
    won't work. The reason is the [] operator when used to create or index a
    static array does something different than it does when being used with a
    pointer variable.

    Technically the reason is that no matter how many [] you use to define a
    multidimensional array, the array is always a flat rendition with one row
    contiguously following the last in memory. But if you declare a pointer
    with multiple stars, what happens is that the pointer points to an array
    of pointers, and each pointer in the indexed arrays points to another
    array of pointers, and so on until you get to the last star where the
    pointers each point to some data corresponding to rows given in the last
    index.

    The way it works out, if you have a single dimensional array the array
    name is equivalent to a pointer to the array because you are already at
    the last level of the pointers. But as you add dimensions to the array,
    the layout grows different than the flat rendition as found in the static
    version of the array, so your code won't work. If the compiler doesn't
    tell you you can't make the assignment to temp, it is probably broken.

    I'm drawing a blank as to how to fix it without resorting to something
    like:

    int *a[10]

    and then allocating the elements for the other array index dynamically

    David

    TLOlczyk wrote:

    > I have a brain cramp and I need some help.
    >
    > I have a chunk of code below which demonstrates
    > a problem I have with multidimensional arrays.
    > I want to keep it simple but something specific is getting in the way.
    >
    > int a[10][10];
    > int b[10][10];
    > int **present;
    > int **next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > for(int j=0;j<10;j++)
    > next[j]=present[j];
    > int **temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }
    >
    > Compare this to the one-dim version ( which works )
    > int a[10];
    > int b[10];
    > int *present;
    > int *next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > next=present;
    > int *temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }
    >
    > The reply-to email address is .
    > This is an address I ignore.
    > To reply via email, remove 2002 and change yahoo to
    > interaccess,
    >
    > **
    > Thaddeus L. Olczyk, PhD
    >
    > There is a difference between
    > *thinking* you know something,
    > and *knowing* you know something.
    David Lindauer, Oct 15, 2004
    #4
  5. TLOlczyk

    JKop Guest


    > int a[10][10];
    > int b[10][10];
    > int **present;


    "present" points to the third ring of Saturn.

    > int **next;


    "next" points to the Pluto's smaller moon.


    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > for(int j=0;j<10;j++)
    > next[j]=present[j];


    So you're setting the contents of Saturn's third ring to the contents of
    Pluto's smaller moon.

    > int **temp=next;


    "temp" now points to Saturn's third ring.

    > next=present;


    "next" now points to Pluto's smaller moon.

    > present=temp;


    etc.

    > test_for_done(present);
    > }
    >
    > Compare this to the one-dim version ( which works )
    > int a[10];
    > int b[10];
    > int *present;
    > int *next;
    >
    > bool done=false;
    >
    > while(!done)
    > {
    > // Loop which substitutes for complex calculation
    > for(int i=0;i<10;i++)
    > next=present;
    > int *temp=next;
    > next=present;
    > present=temp;
    > test_for_done(present);
    > }



    Why are you messing with these pointers?

    Anyway, just for your information, here's how you define a reference to a
    multidimensional array:

    int a[10][10];
    int b[10][10];

    int (&x)[10][10] = a;
    int (&y)[10][10] = b;


    -JKop
    JKop, Oct 15, 2004
    #5
  6. TLOlczyk

    Ron Natalie Guest

    TLOlczyk wrote:
    > I have a brain cramp and I need some help.
    >
    > I have a chunk of code below which demonstrates
    > a problem I have with multidimensional arrays.
    > I want to keep it simple but something specific is getting in the way.
    >


    Other than declaring them, your code doesn't create use arrays
    at all.

    First thing you must realize. ARRAYS and POINTERS are not
    the same thing. Further you can't even convert between
    int array[10][10] and int**
    they are not related types at all.

    Second, let me further point out, that C++ really only has
    single dimensioned arrays. A multidimension array array
    is really an array of arrays.

    int array[5][3]

    array above is a 5 element array of 3 element arrays of int.

    The implicit conversion of int[5][3] to a pointer is not int**,
    it is int (*ptr)[3] (or in English: a pointer to a three element
    array of type int). Every time ptr is incremented, it points at
    the next 3 element array.


    Chew over this and if you have further questions, try to be more
    speicifc.
    Ron Natalie, Oct 15, 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. Roger Leigh
    Replies:
    8
    Views:
    413
    Karl Heinz Buchegger
    Nov 17, 2003
  2. TLOlczyk
    Replies:
    3
    Views:
    412
    Howard
    Oct 14, 2004
  3. Replies:
    3
    Views:
    433
    Victor Bazarov
    Nov 10, 2004
  4. DanielEKFA
    Replies:
    8
    Views:
    584
    DanielEKFA
    May 16, 2005
  5. Replies:
    8
    Views:
    692
    Bruno Desthuilliers
    Dec 12, 2006
Loading...

Share This Page