Following are different ways to create a 2D array on heap (or dynamically allocate a 2D array).
In the following examples, we have considered ‘r‘ as number of rows, ‘c‘ as number of columns and we created a 2D array with r = 3, c = 4 and following values
1 2 3 4 5 6 7 8 9 10 11 12
1) Using a single pointer:
A simple way is to allocate memory block of size r*c and access elements using simple pointer arithmetic.
#include <stdio.h> #include <stdlib.h> int main() { int r = 3, c = 4; int *arr = (int *)malloc(r * c * sizeof(int)); int i, j, count = 0; for (i = 0; i < r; i++) for (j = 0; j < c; j++) *(arr + i*c + j) = ++count; for (i = 0; i < r; i++) for (j = 0; j < c; j++) printf("%d ", *(arr + i*c + j)); /* Code for further processing and free the dynamically allocated memory */ return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12
2) Using an array of pointers
We can create an array of pointers of size r. Note that from C99, C language allows variable sized arrays. After creating an array of pointers, we can dynamically allocate memory for every row.
#include <stdio.h> #include <stdlib.h> int main() { int r = 3, c = 4, i, j, count; 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]); /* Code for further processing and free the dynamically allocated memory */ return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12
3) Using pointer to a pointer
We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.
#include <stdio.h> #include <stdlib.h> int main() { int r = 3, c = 4, i, j, count; 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]); /* Code for further processing and free the dynamically allocated memory */ return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12
4) Using double pointer and one malloc call
#include<stdio.h> #include<stdlib.h> int main() { int r=3, c=4, len=0; int *ptr, **arr; int count = 0,i,j; len = sizeof(int *) * r + sizeof(int) * c * r; arr = (int **)malloc(len); // ptr is now pointing to the first element in of 2D array ptr = (int *)(arr + r); // for loop to point rows pointer to appropriate location in 2D array for(i = 0; i < r; i++) arr[i] = (ptr + c * i); 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]); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12
Thanks to Trishansh Bhardwaj for suggesting this 4th method.
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to dynamically allocate a 3D array in C++
- How to declare a 2D array dynamically in C++ using new operator
- How to make a C++ class whose objects can only be dynamically allocated?
- What’s difference between “array” and “&array” for “int array[5]” ?
- Pointer to an Array | Array Pointer
- array::front() and array::back() in C++ STL
- array::fill() and array::swap() in C++ STL
- array::begin() and array::end() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- Difference between pointer to an array and array of pointers
- Jagged Array or Array of Arrays in C with Examples
- Array of Structures vs. Array within a Structure in C/C++
- Why C treats array parameters as pointers?
- Pointer vs Array in C
- Do not use sizeof for array parameters
- What is the difference between single quoted and double quoted declaration of char array?
- Are array members deeply copied?
- How to pass a 2D array as a parameter in C?

