I know that there have been posted some questions about 2d arrays with malloc, however, I would like to refer to the answer to one of those questions.
I cannot visualise the realisation of the following fragment:
Allocate an array of M rows of N columns like this:
int (*array)[N] = malloc(M * sizeof *array);
- If we have M rows, why does
*array
have N parameter? - What is the size of the
*array
(also as a number)? I understand the result of the part with malloc() only as M*N, but single block (1d) of the structure. - How does M size, passed only in malloc, becomes number of rows? How does it become
*array[M][N]
?
I would appreciate your answers.