Example of C Language Creating Dynamic Two dimensional Array

htt‮:sp‬//www.lautturi.com
Example of C Language Creating Dynamic Two dimensional Array

To create a dynamic array,we use malloc to dynamically allocate memory space:

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i, j, col = 3, row = 4;

	/* Creating Dynamic Two dimensional Array */
  
    int **m = (int**)malloc(col * sizeof(int)); 
    for(int i = 0; i < col ; i++){
        m[i] = (int*)malloc(row * sizeof(int));
    }
  	
	// the code using the array:
	/*
	code
	*/
	
	
  	/* Releases the 2-dimensional array */
  
    for (int i = 0; i < row; i++){
        free(m[i]);
    }
    free(m);
}
Created Time:2017-08-28 15:32:38  Author:lautturi