1.
Introduction
2.
Dynamic Allocation of 2D Array
3.
Using Single Pointer
4.
Using an Array of Pointers
5.
Using pointer to a pointer
6.
FAQs
6.1.
What is pointer to pointer?
6.2.
What is malloc()?
6.3.
What is free()?
6.4.
How to initialize 2D array?
6.5.
How are 2D arrays stored in C?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Dynamically Allocate a 2D Array in C

Toohina Barua
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

A 2D array is a collection of arrays. In C programming, a two-dimensional (2D) array is also known as a matrix. A table of rows and columns can be used to represent a matrix. Take a look at the following C program before learning more about two-dimensional arrays.
In this article, we will look at different ways to create a 2D array dynamically. So let us dive in!

Also See: Sum of Digits in C, C Static Function, And Tribonacci Series

## Dynamic Allocation of 2D Array

We'll look at a few different approaches to creating a 2D array on the heap or dynamically allocate a 2D array.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Using Single Pointer

A single pointer can be used to dynamically allocate a 2D array in C. This means that a memory block of size row*column*dataTypeSize is allocated using malloc, and the matrix elements are accessed using pointer arithmetic.

``````#include <stdio.h>
#include <stdlib.h>
int main() {
int row = 2, col = 3; //number of rows=2 and number of columns=3
int *arr = (int *)malloc(row * col * sizeof(int));
int i, j;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
*(arr + i*col + j) = i + j;
printf("The matrix elements are:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", *(arr + i*col + j));
}
printf("\n");
}
free(arr);
return 0;
}``````

Output:

## Using an Array of Pointers

As shown below, we can dynamically create an array of size M pointers and then dynamically allocate memory of size N for each row:

Source

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

int main()
{
int r = 3, c = 4, i, j, count; //number of rows=3 and number of columns=4

int* arr[r];
for (i = 0; i < r; i++)
arr[i] = (int*)malloc(c * sizeof(int));

// Note that arr[i][j] is same as *(*(arr+i)+j)
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

//  free the dynamically allocated memory

for (i = 0; i < r; i++)
free(arr[i]);

return 0;
}``````

Output:

You can also read about dynamic array in c and  Short int in C Programming

## Using pointer to a pointer

A double pointer can also be used to dynamically create an array of pointers. We can dynamically allocate memory and for each row once we have an array pointer allocated dynamically.

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

int main()
{
int r = 3, c = 4, i, j, count; //number of rows=3 and number of columns=4

int** arr = (int**)malloc(r * sizeof(int*));
for (i = 0; i < r; i++)
arr[i] = (int*)malloc(c * sizeof(int));

// Note that arr[i][j] is same as *(*(arr+i)+j)
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

// free the dynamically allocated memory

for (i = 0; i < r; i++)
free(arr[i]);

free(arr);

return 0;
}``````

Output:

You can practice by yourself with the help of the C online compiler.

Must Read Decision Making in C

## FAQs

### What is pointer to pointer?

Multiple indirection, or a chain of pointers, is pointer to pointer aka double pointer. A pointer normally contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the value's location.

### What is malloc()?

In C, the "malloc" or "memory allocation" method is used to allocate a single large block of memory with the specified size dynamically. It returns a void pointer that can be cast into any type of pointer. It does not initialize memory at execution time, so each block is initially set to the default garbage value.

### What is free()?

The "free" method in C is used to de-allocate memory dynamically. The memory allocated by malloc() and calloc() is not automatically de-allocated. As a result, whenever dynamic memory allocation occurs, the free() method is used. It frees up memory, which helps to reduce memory waste.

### How to initialize 2D array?

The following example demonstrates how to initialize 2D array:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

### How are 2D arrays stored in C?

A 2D array is stored one row after another in the computer's memory. The memory location of the entire 2D array is determined by the address of the first byte of memory.

## Conclusion

In this article, we learned how to dynamically allocate a 2D array in C and saw its practical demonstration.

We hope that this blog has helped you enhance your knowledge regarding how to dynamically allocate a 2D array in C and if you would like to learn more about arrays & practice the same, check out the following problems based on array -

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass