0

I want to know the difference between theses two declarations of these arrays, one is defined like this (double array[a][b][c][d]), the other one is different (double array[a*b*c][d])

double weight4_5[LAYER4][LAYER5][LENGTH_KERNEL][LENGTH_KERNEL];
double weight5_6[LAYER5 * LENGTH_FEATURE5 * LENGTH_FEATURE5][OUTPUT];

if i want to load values to these two arrays with for loops, how can i do it ?

B.Y
  • 39
  • 2
  • 9
  • Possible duplicate of [Difference between multi dimensional arrays and array of pointers](https://stackoverflow.com/questions/17823058/difference-between-multi-dimensional-arrays-and-array-of-pointers) – Mateen Ulhaq Sep 27 '19 at 00:42
  • Also see https://stackoverflow.com/questions/17808535/runtime-error-in-the-following-code and https://stackoverflow.com/questions/17752549/pointer-expressions-ptr-ptr-and-ptr-use – Mateen Ulhaq Sep 27 '19 at 00:42
  • 1
    They're the same in terms out memory layout. However, the first has the obvious advantage that you can index it more easily. – Mateen Ulhaq Sep 27 '19 at 00:44

1 Answers1

1

The first one is a 4 Dimensional array.

  • Access is by weight4_5[i][j][k][l]

The second one is a 2 Dimensional array. It is a flattened array.

  • Access is basically by weight5_6[x][y] But you need to do some calculations to get the value of x based on the 4D original array.
  • You can organise it multiple ways as you want, (e.g. row wise, column wise etc) One way is to have

    x= ((((i*LAYER5)+j)*LENGTH_FEATURE5)+k)
    y = l;
    
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31