0

I have a pointer to 4dimensional array; and I want to use it to assign values to 4 dimensional array.

What I know is to create a 4 dimensional array and traverse through it using pointer to float

float dArray [2][3][4][5];
float *ptr = &dArray[0][0][0][0];

What I am struggling with is if I have pointer to 4dimensional array

float ***pointerArray4D[];

and say I want to use it store values in it for below array - how do I do it.

value = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
 for (int ix1 = 0; ix1 < DIM1; ++ix1)
  for (int ix2 = 0; ix2 < DIM2; ++ix2)
   for (int ix3 = 0; ix3 < DIM3; ++ix3)
    pointerArray4D[ix0][ix1][ix2][ix3] = float(value++); 

What I have tried to do is

1)

float pointerArray4D_1[2][3][4][5];
float ***pointerArray4D[5];
pointerArray4D = pointerArray4D_1;

2) Other thing which i think I should be doing is assign memory to it

pointerArray4D=new float*[2]; 
for(int i=0; i<2; ++i)
    pointerArray4D[i]=new float*[3];
for(int i=0; i<3; ++i)
    pointerArray4D[i]=new float*[4];
for(int i=0; i<4; ++i)
    pointerArray4D[i]=new float[5];

error: expected constructor, destructor, or type conversion before ‘=’ token

Needless to say a new bee learning the art :)

Edited - the typo of using double to represent the idea of I being able to traverse

M.M
  • 138,810
  • 21
  • 208
  • 365
oneday
  • 629
  • 1
  • 9
  • 32

2 Answers2

0

This ...

double dArray [2][3][4][5];

... declares an array. There is storage for 2 * 3 * 4 * 5 doubles associated with it.

This ...

float ***pointerArray4D[];

... declares a an array of unspecified length of float ***, where it's in fact legal at all (that is, at file scope). That's effectively the same as a float ****. There is no associated storage for any actual floats until and unless you allocate some. Moreover, the space you allocate for pointerArray4D would be expected to hold values of type float *** (per its declaration). You can work with this, but you'd have to perform four levels of allocation.

Supposing DIM0 - DIM3 to be preprocessor macros, you maybe want something like this, instead:

float (*pointerArray4D)[DIM1][DIM2][DIM3];

pointerArray4D = malloc(DIM0 * sizeof(*pointerArray4D));

... after which you can handle pointerArray4D exactly as you would do dArray. (But don't forget to free(pointerArray4D) when you're done with it.)

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Note that this answer assumes C, which is not the same thing as C++. – John Bollinger Nov 03 '14 at 22:20
  • `float ***pointerArray4D[];` does not declare a `float ****`. In C++ it's illegal. In C it is legal at file scope and is a tentative definition of an array of `float ***`, however OP seems to be using it at block scope anyway. – M.M Nov 03 '14 at 22:57
  • @MattMcNabb, I did note already that my answer pertains to C, not C++. With regard to C, cdecl calls `float ***pointerArray4D[]` the declaration of an *array* of `float ***`, which decays to `float ****`, not `float ***`. I admit to some imprecision there, but for most intents and purposes, `float ****` can be used in place of `float ***pointerArray4D[]` anywhere the latter is allowed in the first place. – John Bollinger Nov 03 '14 at 23:18
0

First of all, following the hint of user3353819:

think of double array[a][b][c][d] as simply an array of doubles of length a * b * c * d. This means you would find the first element with &array which is of type double*

Looking at your code:

float ***pointerArray4D[];
value = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
 for (int ix1 = 0; ix1 < DIM1; ++ix1)
  for (int ix2 = 0; ix2 < DIM2; ++ix2)
   for (int ix3 = 0; ix3 < DIM3; ++ix3)
    pointerArray4D[ix0][ix1][ix2][ix3] = float(value++); 

the right way to do it is:

float fArray[DIM0][DIM1][DIM2][DIM3];
float *pointerArray = &fArray[0][0][0][0];
float fValue = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
 for (int ix1 = 0; ix1 < DIM1; ++ix1)
  for (int ix2 = 0; ix2 < DIM2; ++ix2)
   for (int ix3 = 0; ix3 < DIM3; ++ix3)
    pointerArray[ ix0*DIM1*DIM2*DIM3 + ix1*DIM2*DIM3 + ix2*DIM3 + ix3 ] = fValue++;

The key is that the array having dimension of [DIM0][DIM1][DIM2][DIM3] is a contiguos space in memory that hold "DIM0 times a block containing" "DIM1 times a block containing" "DIM2 times a block containing" "DIM3 times a float"

As esemplification the following code give the same result:

float fArray[DIM0][DIM1][DIM2][DIM3];
float *pointerArray = &fArray[0][0][0][0];
float fValue = 0;
for (int ix0 = 0; ix0 < DIM0; ++ix0)
 for (int ix1 = 0; ix1 < DIM1; ++ix1)
  for (int ix2 = 0; ix2 < DIM2; ++ix2)
   for (int ix3 = 0; ix3 < DIM3; ++ix3)
    *pointerArray++ = fValue++;

or even better:

float fArray[DIM0][DIM1][DIM2][DIM3];
float *pointerArray = &fArray[0][0][0][0];
float fValue = 0;
const int nElem = DIM0*DIM1*DIM2*DIM3;
for (int ix = nElem; ix>0; --ix)
   *pointerArray++ = fValue++;

Hoping this answer you question, have a good night, Stefano

Stefano Buora
  • 1,052
  • 6
  • 12