References to multidimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TLOlczyk

    #1

    References to multidimensional array

    I have a brain cramp and I need some help.

    I have a chunk of code below which demonstrates
    a problem I have with multidimensiona l 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[i][j]=present[i][j];
    int **temp=next;
    next=present;
    present=temp;
    test_for_done(p resent);
    }

    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[i]=present[i];
    int *temp=next;
    next=present;
    present=temp;
    test_for_done(p resent);
    }




    The reply-to email address is olczyk2002@yaho o.com.
    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.
  • Victor Bazarov

    #2
    Re: References to multidimensiona l array

    TLOlczyk wrote:[color=blue]
    > I have a brain cramp and I need some help.
    >
    > I have a chunk of code below which demonstrates
    > a problem I have with multidimensiona l 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;[/color]

    This should not work. Pointers to pointers are not compatible with
    two-dimensional arrays.
    [color=blue]
    >
    > 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[i][j]=present[i][j];
    > int **temp=next;
    > next=present;
    > present=temp;
    > test_for_done(p resent);
    > }[/color]

    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)[i][j]=(*present)[i][j];
    std::swap(prese nt, next);
    done = test_for_done(p resent);
    }
    [color=blue]
    >
    > [...][/color]

    Victor

    Comment

    • Howard

      #3
      Re: References to multidimensiona l array


      "TLOlczyk" <olczyk2002@yah oo.com> wrote in message
      news:27utm01ti3 f1jtg29gre90khf j1ubdcgmf@4ax.c om...[color=blue]
      >I have a brain cramp and I need some help.
      >
      > I have a chunk of code below which demonstrates
      > a problem I have with multidimensiona l 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;
      >[/color]

      Ok, at least that *attempts* to initialize something. But does it even
      compile? The variable a is not an int**.
      [color=blue]
      > 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[i][j]=present[i][j];
      > int **temp=next;
      > next=present;
      > present=temp;
      > test_for_done(p resent);[/color]

      Is this where done gets set? If so, how? Is it really "done =
      test_for_done(p resent);"?
      [color=blue]
      > }
      >
      > 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[i]=present[i];
      > int *temp=next;
      > next=present;
      > present=temp;
      > test_for_done(p resent);
      > }[/color]

      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.




      Comment

      • TLOlczyk

        #4
        Re: References to multidimensiona l array

        On Thu, 14 Oct 2004 18:23:49 -0400, Victor Bazarov
        <v.Abazarov@com Acast.net> wrote:
        [color=blue]
        >What you need is a pointer to an array:
        >
        > int (*present)[10][10] = &a;
        > int (*next)[10][10] = &b;[/color]

        Exactly.Thanks.


        The reply-to email address is olczyk2002@yaho o.com.
        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.

        Comment

        Working...