I\'m trying to allocate a 2d array in a C program. It works fine in the main function like this (as explained here):
#include
#include
Shorter should be below, with free (!) and initialized values for each grid element:
#include
#include
#define NROWS 10
#define NCOLS 10
int main()
{
int (* grid)[NCOLS] = calloc(NROWS,sizeof*grid);
/* no more needed here malloc2d(grid, 10, 10); */
grid[5][6] = 15;
printf("%d\n", grid[5][6]);
free(grid); /* every c/malloc need a free */
return 0;
}