malloc for 2d array code example
Example 1: double pointer malloc in c
char *x;
char **y;
x = (char*)malloc(sizeof(char) * 100);
y = (char**)malloc(sizeof(char*) * 100);
y = (char**)malloc(sizeof(char) * 50 * 50);
Example 2: c malloc for 2d array
#include <stdlib.h>
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}
for(i = 0; i < nrows; i++)
{
array[i] = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}
}