casting void** to 2D array of int – C

The compiler is right, an array of arrays (or a pointer to an array) is not the same as a pointer to a pointer. Just think about how they would be laid out in memory:

A matrix of size MxN in the form of an array of arrays:

+--------------+--------------+-----+----------------+--------------+-----+------------------+
| matrix[0][0] | matrix[0][1] | ... | matrix[0][N-1] | matrix[1][0] | ... | matrix[M-1][N-1] |
+--------------+--------------+-----+----------------+--------------+-----+------------------+

A and the same “matrix” in the form of pointer to pointer:

+-----------+-----------+-----------+-----+
| matrix[0] | matrix[1] | matrix[2] | ... |
+-----------+-----------+-----------+-----+
  |           |           |
  |           |           V
  |           |           +--------------+--------------+-----+
  |           |           | matrix[2][0] | matrix[2][1] | ... |
  |           |           +--------------+--------------+-----+
  |           |
  |           V
  |           +--------------+--------------+-----+
  |           | matrix[1][0] | matrix[1][1] | ... |
  |           +--------------+--------------+-----+
  |
  V
  +--------------+--------------+-----+
  | matrix[0][0] | matrix[0][1] | ... |
  +--------------+--------------+-----+

It doesn’t matter if you allocate the correct size, the two variables simply are incompatible which is what your compiler is telling you.

Leave a Comment