Click to See Complete Forum and Search --> : multidimensional array indirection


jcstiff
June 5th, 2002, 11:57 PM
Does anyone know how to traverse (recursively or otherwise) a multidimensional array to get to the singleton dimension?
Or just get all the values out for that matter...

What I mean is this:

Say a function returned to me an N-D array, by reference, at runtime. No static arrays declared for storing this N-D array, all dynamic.
What I can do at runtime is retrieve the number of dimensions, and the number of rows/columns of per dimension (above 2-D of course). For example, a 3x3x3 array would look like this:
[:,:,1]
x x x
x x x
x x x
[:,:,2]
x x x
x x x
x x x
[:,:,3]
x x x
x x x
x x x

In the above example, the 3-D array has 27 values. If I called this array 'duh', I could print out all values contained in 'duh' by a for loop along the singleton pointer dimension:

memcpy( duh, dv_array, num_elems * sizeof( int ) );

for( i = 0; i < num_elems; i++ )
{
//<***duh> is the singleton dimension
printf( "%d\n", ***duh+i );
}


Now the problem occurs, since this retrieval is dynamic
(all I have is the base pointer of my N-D array), how do I get n *'s in front of my variable name to get to that singleton pointer dimension for printing and other purposes?

Is there a way to make pointer keep pointing to one past itself inside a loop?

Since I have the number of dimensions, I want to say one for loop should would work, but I am not sure how to make it do so.

If anyone can help me with this, I would jump and down and be very giddy.

Na-iem
June 6th, 2002, 10:14 AM
Try this:

//start source code
// main.cpp
#include "stdio.h"

int main(int argc, char* argv[])
{

int test[3][3][3] = { {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}},
{{9, 10, 11}, {12, 13, 14}, {15, 16,17}},
{{18, 19, 20}, {21, 22, 23}, {24, 25, 26}}
};

int x = sizeof(test);
int *p= (int*)&test;
int size = 3;

for(int i=0; i< size; ++i)
{
for(int j=0; j< size; ++j)
{
for(int k=0; k< size; ++k)
{
printf("The value at position (%d, %d, %d) is [%d]\n", i, j, k, *p++);


}

}


}

return 0;
}

//end source code

Be careful, since this requires you multi dimensional array to be stored in a contiguous block of memory. If you are sure of this, then the above should always work.

Now you can go jump up and down and be giddy... just don't go jump in a lake :-)

Na-iem

JMS
June 6th, 2002, 06:02 PM
Don't think of the array as a three dementional array because that's really nothing but a mediphore. All structures are just memory and all memory is just flat addressed sequencially.

At least now that dos segmented memory is a thing of the past. Yet I digress because even dos segmented memory could only be addressed sequencially......

Anyways your compiler makes the calculations for you when you derefference a multidementional array like Array[1][1][1].

Array is actually a pointer to memory and your compiler makes the calculation to get you to the right location. If you know the size of each dynamic demension at run time you can make the calculation yourself..


int Array3[x][y][z];

can thus be accessed like this

int *pArray = (int *)Array3;

// going to location Array3[1][2][3]

// x y z
int iLocation_1_2_3 = pArray[ ((y*z)*1) + (z*2) + 3 ];

Draw your three demensional array out on paper. If you're not an artistic guy try a two demensional array which would be easier to draw. Then draw it as a single contigous 1 demensional string of numbers, which is really what it is anyway. Then you can see what I did and it will make sense to you....


// here is a working program to play with which illistrates the
// principle

int OutPutMyValue ( int iX, int iY, int iZ, int *pMyArray );


void main()
{
int iValue = 0;
int MyArray[10][9][8];
int *pMyArray = (int *)MyArray;

for ( int x = 0; x < 10; x++ )
for ( int y = 0; y < 9; y++ )
for ( int z = 0; z < 8; z++ )
MyArray[x][y][z] = iValue++;

OutPutMyValue ( 10, 9, 8, pMyArray);
}


int OutPutMyValue ( int iX, int iY, int iZ, int *pMyArray )
{
for ( int x = 0; x < 10; x++ )
for ( int y = 0; y < 9; y++ )
for ( int z = 0; z < 8; z++ )
{
printf( "Value of x = [%d], y = [%d], z = [%d] = [%d]\n",
x,y,z, pMyArray[ (iY*iZ)*x + iZ*y + z ] );
}

return 1;
}

jcstiff
June 6th, 2002, 07:20 PM
Sweet, thanks everyone... I got it to work