Danijel
Danijel

Reputation: 8610

Converting 2D array into 3D?

Is this a proper way of taking half of 2D array and "converting" it to a 3D array?

#define CH_2D (6)
#define CH_3D (3)

float   w_2d [65*CH_2D][2];
float (*w_3d)[65][CH_3D][2] = (void *) w_2d;

Will this access the right elements of original w_2d:

for(int i=0; i<65; i++) 
{
   for(int j=0; j<CH_3D; j++)
      for(int k=0; k<2; k++)
         float temp = (*w_3d)[i][j][k]; 
}

Upvotes: 1

Views: 103

Answers (1)

user694733
user694733

Reputation: 16047

I believe you have 2 issues with your attempt.

Is this a proper way of taking half of 2D array and "converting" it to a 3D array?

It's not. You are dereferencing pointer with type float[65][3][2], when the actual type is float[390][2], which is strict aliasing violation. To get around this issue, you need to put your C compiler to non-standard mode by disabling strict aliasing, or just rewrite your code to not do that.

Will this access the right elements of original w_2d:

It depends on how you have stored your data on the original array. It might work if the data is stored in order similar to:

float[2][65][3][2]

However, from your w_2d declaration is suspect that your data is arranged like:

float[65][6][2] 
      or
float[6][65][2]

In this case I doubt that you will get the elements you want.

Upvotes: 1

Related Questions