Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > References to multidimensional array

Reply
Thread Tools

References to multidimensional array

 
 
TLOlczyk
Guest
Posts: n/a
 
      10-14-2004
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[i][j]=present[i][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[i]=present[i];
int *temp=next;
next=present;
present=temp;
test_for_done(present);
}




The reply-to email address is http://www.velocityreviews.com/forums/(E-Mail Removed).
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-14-2004
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[i][j]=present[i][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)[i][j]=(*present)[i][j];
std::swap(present, next);
done = test_for_done(present);
}

>
> [...]


Victor
 
Reply With Quote
 
 
 
 
TLOlczyk
Guest
Posts: n/a
 
      10-14-2004
On Thu, 14 Oct 2004 18:23:49 -0400, Victor Bazarov
<(E-Mail Removed)> 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 (E-Mail Removed).
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.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-14-2004

"TLOlczyk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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[i][j]=present[i][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[i]=present[i];
> 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.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
References to multidimensional array TLOlczyk C++ 5 10-15-2004 01:53 PM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments