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[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;
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 ol********@yahoo.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.