multidimensional array indirection
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.
To add to what Na-im said.. which was all correct..
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;
}