Accessing a one-dimensional array as a two-dimensional array Accessing a one-dimensional array as a two-dimensional array arrays arrays

Accessing a one-dimensional array as a two-dimensional array


Question 1:

MATRIXP(matrix) m (matrix should be a variable of type matrix_t) will be expanded to

__typeof__(__typeof__(__typeof__(*((matrix).data))[(matrix).cols]) *) m

From in side to out side

  1. __typeof__(*((matrix).data)) is the type of *((matrix).data), which is int according to the definition of matrix_t.

  2. so (__typeof__(*((matrix).data))[(matrix).cols]) equals (int [cols])

  3. so __typeof__(__typeof__(*((matrix).data))[(matrix).cols]) *) equals (int [cols] *)

  4. and that is the type given by MATRIXP(matrix).

Therefore, MATRIXP(matrix) equals (int [(matrix).cols] *). In other words,

MATRIXP(matrix) m

is effectively

int (*m)[matrix.cols]

As pointed out by @codebeard

Question 2:

It looks quite safe to me.