I have a question about a code snippet provided by following post: https://stackoverflow.com/a/3912959/3668527
int (*array)[cols] = malloc(rows * cols * sizeof(array[0][0]));
Now, I figured out a few days ago that *array[rows]
would mean an array of pointers which each points to a one-dimensional array allocated with malloc
. These arrays are scattered everywhere in the memory.
It would be the same as if I did the following, except that for the former I don't need to free()
the pointer array while for the following, I have to free everything.
int **array = (int **)malloc(sizeof *array * rows);
(I included the cast of the malloc
, because the compiler helped me understand this by directing me what type the variable is. Otherwise it would have been more difficult to understand this.)
But the code snippet at the top is completely different. Could someone tell me how the top code snippet is exactly called, so that I can do some reading on it? When I search for dynamically allocated VLAs, I can only find information about array[rows][cols]
. I cannot find this pointer in (
and )
brackets which can allocate a block of memory just like array[rows][cols]
, but don't need rows
and cols
to be known at compile time.
Edit: My question is a duplicate of pointer to array type, c