by sigsev_251 · 1y ago · 0 comments
Although at least with 2d arrays I prefer to just use a 1d array and index it with [x * width + y], because one problem with multidimensional arrays in C is they need multiple allocations/frees.
edit: Isn't it just:
float (*arr)[m][n] = malloc(sizeof(*arr));But it seems to work, which I didn't expect[0]. It also seems like you have to de-reference it to use it which is weird.
In your code link you over allocate memory, sizeof *arr is enough and you need to dereference like with (*arr)[i][j]. You need to dereference it because it is a pointer to an array, if you dereference you get an array. You can also let the first dimensions decay then it looks like:
double (*arr)[m] = malloc(n * sizeof *arr);
arr[i][j] = ...
but this is not as safe because the bound of the outermost dimension is lost. (Edited)