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