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;

    // Sorry I left these two lines out.
    present=a;
    next=b;

    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;

    present=a;
    next=b;

    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;
    >
    > // Sorry I left these two lines out.
    > present=a;
    > next=b;


    This should not work. Pointers to pointers are not compatible with
    two-dimensional arrays.

    >
    > 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);
    > }


    What you need is a pointer to an array:

    int (*present)[10][10] = &a;
    int (*next)[10][10] = &b;

    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];
    std::swap(present, next);
    done = test_for_done(present);
    }

    >
    > [...]


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

  3. TLOlczyk

    TLOlczyk Guest

    On Thu, 14 Oct 2004 18:23:49 -0400, Victor Bazarov
    <> wrote:

    >What you need is a pointer to an array:
    >
    > int (*present)[10][10] = &a;
    > int (*next)[10][10] = &b;


    Exactly.Thanks.


    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
    #3
  4. 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;
    >
    > // Sorry I left these two lines out.
    > present=a;
    > next=b;
    >


    Ok, at least that *attempts* to initialize something. But does it even
    compile? The variable a is not an int**.

    > 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);


    Is this where done gets set? If so, how? Is it really "done =
    test_for_done(present);"?

    > }
    >
    > Compare this to the one-dim version ( which works )
    > int a[10];
    > int b[10];
    > int *present;
    > int *next;
    >
    > present=a;
    > next=b;
    >
    > 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);
    > }


    I don't see why you're using the pointers at all, in either example.

    All you seem to be doing (or at least attempting) is assigning the array
    members of b to a, then copying them back to a again, and back to b again,
    etc. After the first time through the while loop, both arrays will have the
    same values, so I'm quite confused why you'd need to do it again!

    If you want to use pointers (lord knows why), then perhaps what you want is
    to have an array of pointers, each of which points to an array of integers?
    But the only time I've ever done that is when dynamically allocating the
    arrays in the first place. If you've got arrays already, why mess with
    pointers?

    Again, perhaps you need to explain exactly what you're attempting here.
    Howard, Oct 14, 2004
    #4
    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:
    420
    Karl Heinz Buchegger
    Nov 17, 2003
  2. TLOlczyk
    Replies:
    5
    Views:
    417
    Ron Natalie
    Oct 15, 2004
  3. Replies:
    3
    Views:
    438
    Victor Bazarov
    Nov 10, 2004
  4. DanielEKFA
    Replies:
    8
    Views:
    590
    DanielEKFA
    May 16, 2005
  5. Replies:
    8
    Views:
    696
    Bruno Desthuilliers
    Dec 12, 2006
Loading...

Share This Page