Answers for "malloc in c for 2d array"

C
2

c malloc for 2d array

#include <stdlib.h>

	int **array;
	array = malloc(nrows * sizeof(int *));
	if(array == NULL)
		{
		fprintf(stderr, "out of memoryn");
		exit or return
		}
	for(i = 0; i < nrows; i++)
		{
		array[i] = malloc(ncolumns * sizeof(int));
		if(array[i] == NULL)
			{
			fprintf(stderr, "out of memoryn");
			exit or return
			}
		}
Posted by: Guest on May-07-2021
0

how to malloc for matrix in c

int** mat = (int**)malloc(rows * sizeof(int*))

for (int index=0;index<row;++index)
{
    mat[index] = (int*)malloc(col * sizeof(int));
}
Posted by: Guest on November-27-2020

Code answers related to "C"

Browse Popular Code Answers by Language