c malloc for 2d array

Solutions on MaxInterview for c malloc for 2d array by the best coders in the world

showing results for - "c malloc for 2d array"
Melvyn
10 Feb 2020
1	#include <stdlib.h>
2
3	int **array;
4	array = malloc(nrows * sizeof(int *));
5	if(array == NULL)
6		{
7		fprintf(stderr, "out of memory\n");
8		exit or return
9		}
10	for(i = 0; i < nrows; i++)
11		{
12		array[i] = malloc(ncolumns * sizeof(int));
13		if(array[i] == NULL)
14			{
15			fprintf(stderr, "out of memory\n");
16			exit or return
17			}
18		}
19