int (*mapTerrain)[10] = (int (*)[10])malloc(sizeof(int[10][10]));
free(mapTerrain);
Someone on this site suggested these 2 lines for working with dynamical 2d arrays in C. Dimensions are [10][10]
. Problem is, I'm not sure I understand them correctly. If I had to explain these 2 lines I'd say the following:
On the left we have an array of int
pointers with size 10. (Can't explain the casting, I myself would expect it to be int *
).
What's being passed to malloc
is an array of int
-s sized [10][10]
. (Why isn't it ...malloc(sizeof(int*10*10));
?) What allows us to pass an array to malloc
instead of size_t size
?
As for the free(mapTerrain);
line. How come one free
is enough? From what I remember you have to call free
for every row of a dynamical 2d array.