References to multidimensional array

T

TLOlczyk

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 (e-mail address 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.
 
V

Victor Bazarov

TLOlczyk said:
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
 
T

TLOlczyk

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 address 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.
 
H

Howard

TLOlczyk said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top