Dynamically allocate memory for a 2D array in C
This post will discuss various methods to dynamically allocate memory for 2D array in C using Single Pointer, Array of Pointers, and Double Pointer.
1. Using Single Pointer
In this approach, we simply allocate memory of size M × N dynamically and assign it to the pointer. Even though the memory is linearly allocated, we can use pointer arithmetic to index the 2D array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <stdlib.h> // `M × N` matrix #define M 4 #define N 5 // Dynamically allocate memory for 2D Array int main() { // dynamically allocate memory of size `M × N` int* A = (int*)malloc(M * N * sizeof(int)); if (A == NULL) { fprintf(stderr, "Out of memory"); exit(0); } // assign values to the allocated memory for (int r = 0; r < M; r++) { for (int c = 0; c < N; c++) { *(A + r*N + c) = rand() % 100; } } // print the 2D array for (int r = 0; r < M; r++) { for (int c = 0; c < N; c++) { printf("%d ", (A + r*N)[c]); // or, use *(A + r*N + c) } printf("\n"); } // deallocate memory free(A); return 0; } |
2. Using Array of Pointers
We can dynamically create an array of pointers of size M and then dynamically allocate memory of size N for each row, as shown below:

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <stdlib.h> // `M × N` matrix #define M 4 #define N 5 // Dynamically allocate memory for 2D Array int main() { // dynamically create an array of pointers of size `M` int **A = (int **)malloc(M * sizeof(int *)); // or, use `int* A[M]` if (A == NULL) { fprintf(stderr, "Out of memory"); exit(0); } // dynamically allocate memory of size `N` for each row for (int r = 0; r < M; r++) { A[r] = (int *)malloc(N * sizeof(int)); if (A[r] == NULL) { fprintf(stderr, "Out of memory"); exit(0); } } // assign values to the allocated memory for (int r = 0; r < M; r++) { for (int c = 0; c < N; c++) { A[r][c] = rand() % 100; // or *(A[r] + c) or *(*(A + r) + c) } } // print the 2D array for (int r = 0; r < M; r++) { for (int c = 0; c < N; c++) { printf("%d ", A[r][c]); // or *(A[r] + c) or *(*(A + r) + c) } printf("\n"); } // deallocate memory for (int r = 0; r < M; r++) { free(A[r]); } free(A); return 0; } |
3. Using Double Pointer
We start by creating an array of pointers of size M as seen in the 2nd method. Then dynamically allocate memory of size M × N and let *A point to it. Finally, position allocated memory across M pointers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <stdio.h> #include <stdlib.h> // `M × N` matrix #define M 4 #define N 5 // Dynamically allocate memory for 2D Array int main() { // dynamically create an array of pointers of size `M` int **A = (int **)malloc(M * sizeof(int *)); // or, use `int* A[M]` if (A == NULL) { fprintf(stderr, "out of memory\n"); exit(0); } // dynamically allocate memory of size `M × N` and let *A point to it *A = (int *)malloc(sizeof(int) * M * N); // position allocated memory across `M` pointers for (int r = 0; r < M; r++) { A[r] = (*A + N*r); } // assign values to the allocated memory for (int r = 0; r < M; r++) { for (int c = 0; c < N; c++) { A[r][c] = rand() % 100; // or *(A[r] + c) or *(*(A + r) + c) } } // print the 2D array for (int r = 0; r < M; r++) { for (int c = 0; c < N; c++) { printf("%d ", A[r][c]); // or *(A[r] + c) or *(*(A + r) + c) } printf("\n"); } // deallocate memory free(*A); free(A); return 0; } |
That’s all about dynamically allocating memory for a 2D array in C.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)