C Programming

How to Create 2 Dimensional Array Using Malloc() in C Programming

The malloc() function is used in c programming to store the data in the heap which is dynamic memory storage. It is mostly used for the dynamic declaration of the arrays and also be used for the creation of two-dimensional arrays. The two-dimensional arrays are used to plot the values in the tabular form having the columns and rows.

In this write-up, we will learn to create a 2-Dimensional array using the malloc() function in C programming.

What is a malloc() function in c programming

In some specific programs, we often can’t predict the size of an array. If we assign the values to the array by ourselves then it cannot be changed during the execution. This will create problems, either the memory will become low for the array or the array will occupy less space wasting the memory so to avoid this, it is recommended to assign arrays dynamically.

In C programming, for dynamic memory allocation, different functions are used. One of them is the malloc() function; it sends a request to the heap for a specific block of memory and if the heap has the space, it responds by allocating the requested block of memory to malloc().

The malloc() will occupy the space of the assigned memory block according to the need of an executed program and the after the successful execution, this space can be made free by using the free() function.

How to create the 2-dimensional array using the malloc() function in C programming

Before creation,  consider the following figure for a better understanding of how the 2-dimensional array works.

In the above figure, we can understand that in a 2-dimensional array, first the elements will be fixed in the first box of the column after this corresponding row will be filled, and then the second row of the column and it goes on till the whole array is filled like first elements will be placed in x[0][0[, then x[0][1], then [0][2], then x[0][1], [1][0], and then so on.

We will consider the following basic example of creating a 2-dimensional array using the malloc() in c programming. We created a text file, cfile.c and type the following script in it:

#include <stdio.h>
#include <stdlib.h>
int main() {

   int row = 5, col = 4;
   int *a = (int *)malloc(row * col * sizeof(int));

   int i, j;
   for (i = 0; i < row; i++)
      for (j = 0; j < col; j++)
         *(a + i*col + j) = i + j;    

   printf("The array elements are:\n");
   for (i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
         printf("%d ", *(a + i*col + j));
   return 0;

To compile the above program, we will use the GCC compiler:

$ gcc cfile.c -o cfile

Execute the cfile, which is the compiled file of cfile.c and display the output:

$ ./cfile

In the above code, we have included the libraries of stdlib.h (for dynamic memory allocation) and stdio.h (for the input and output), then we declared the variables row, column, i, and j. After the declaration, we called the malloc() to assign the memory according to the values of “row” and “column”. Once the memory was allocated, we simply used the two nested loops to save the values in the array, and then using the printf() in the nested loop we displayed the values. In the end, we used the free() to empty the occupied space assigned on the heap by malloc().


Two-dimensional arrays are similar to one-dimensional arrays, the only difference between the 1D and 2D arrays is; the 2D arrays store the data in the tabular form, and the 1D array stores data in the form of rows. The arrays are the basics of the data structures and they can store a large amount of data of the same data type. In this article, we have explained the creation of the 2-dimensional array in the dynamic memory by calling the malloc() function.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.