How to Allocate 2D Array Dynamically in C: Syntax and Example
To allocate a 2D array dynamically in C, use
malloc to allocate memory for an array of pointers (rows), then allocate memory for each row's columns. This creates a flexible 2D array where sizes can be set at runtime.Syntax
To dynamically allocate a 2D array in C, you first allocate memory for an array of pointers (each pointer represents a row). Then, for each row, allocate memory for the columns. Finally, you can access elements using array[row][column].
int **array;declares a pointer to pointer for 2D array.array = malloc(rows * sizeof(int *));allocates memory for row pointers.array[i] = malloc(cols * sizeof(int));allocates memory for each row's columns.
c
int **array; int rows = 3, cols = 4; array = malloc(rows * sizeof(int *)); for (int i = 0; i < rows; i++) { array[i] = malloc(cols * sizeof(int)); }
Example
This example shows how to allocate a 3x4 2D array dynamically, fill it with values, print them, and then free the memory.
c
#include <stdio.h> #include <stdlib.h> int main() { int rows = 3, cols = 4; int **array = malloc(rows * sizeof(int *)); if (array == NULL) { printf("Memory allocation failed\n"); return 1; } for (int i = 0; i < rows; i++) { array[i] = malloc(cols * sizeof(int)); if (array[i] == NULL) { printf("Memory allocation failed\n"); // Free previously allocated rows for (int j = 0; j < i; j++) { free(array[j]); } free(array); return 1; } } // Fill the array for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { array[i][j] = i * cols + j + 1; } } // Print the array for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", array[i][j]); } printf("\n"); } // Free memory for (int i = 0; i < rows; i++) { free(array[i]); } free(array); return 0; }
Output
1 2 3 4
5 6 7 8
9 10 11 12
Common Pitfalls
Common mistakes when dynamically allocating 2D arrays include:
- Not checking if
mallocreturnsNULL, which means allocation failed. - Forgetting to allocate memory for each row separately.
- Not freeing all allocated memory, causing memory leaks.
- Confusing the order of allocation or accessing invalid indexes.
Always free each row's memory before freeing the array of pointers.
c
/* Wrong: Allocating only one block without pointers for rows */ int *array = malloc(rows * cols * sizeof(int)); // Accessing as array[i][j] will cause errors /* Right: Allocate array of pointers, then each row */ int **array = malloc(rows * sizeof(int *)); for (int i = 0; i < rows; i++) { array[i] = malloc(cols * sizeof(int)); }
Quick Reference
- Use
int **arrayfor dynamic 2D arrays. - Allocate rows first, then columns for each row.
- Always check if
mallocreturnsNULL. - Free memory in reverse order: free each row, then the array pointer.
Key Takeaways
Dynamically allocate a 2D array by first allocating an array of pointers for rows.
Allocate memory for each row separately to create columns.
Always check if memory allocation succeeds by verifying
malloc return values.Free all allocated memory to avoid leaks, freeing rows before the main pointer.
Access elements using
array[row][column] syntax after allocation.