Featured Article

Mastering Dynamic 2D Arrays in C++: An Example-Based Guide

William Aug 14, 2026

Dynamic 2D arrays in C++ are a powerful tool, allowing efficient manipulation and storage of multi-dimensional data. They offer flexibility, as their size can change at runtime, unlike static arrays. Let's dive into an example demonstrating how to create, initialize, and manipulate a dynamic 2D array in C++.

How to Print 2D Array in C
How to Print 2D Array in C

Before we proceed, ensure you have a basic understanding of pointers, memory allocation, and dynamic arrays in C++. If not, brush up on those topics to make the most of this guide.

What is a Multidimensional array in C?
What is a Multidimensional array in C?

Creating a Dynamic 2D Array

A dynamic 2D array in C++ is essentially a pointer to a pointer, where each pointer points to an array of integers (or any data type). Here's how you can create one:

C Arrays (With Examples)
C Arrays (With Examples)

```cpp int** dynamic2DArray; // Declare a pointer to a pointer ```

Allocating Memory

C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)

Next, we allocate memory for this 2D array using new. The size of the first dimension can be specified at runtime, giving our array dynamic functionality:

```cpp int rows = 5; // Number of rows, change this as needed dynamic2DArray = new int*[rows]; // Allocate memory for rows for(int i = 0; i < rows; i++) { dynamic2DArray[i] = new int[column]; // Allocate memory for columns (assuming column is a global or predefined constant) } ```

Initializing and Using the Array

C Programming - 2D Arrays
C Programming - 2D Arrays

Now that the array has been allocated, we can initialize and use it:

```cpp for(int i = 0; i < rows; i++) { for(int j = 0; j < column; j++) { dynamic2DArray[i][j] = i + j; // Initialize or assign values } } // Accessing the array for(int i = 0; i < rows; i++) { for(int j = 0; j < column; j++) { cout << dynamic2DArray[i][j] << " "; // Print the values } cout << endl; } ```

Resizing a Dynamic 2D Array

Multidimensional Array in Java (2D Array) - Scientech Easy
Multidimensional Array in Java (2D Array) - Scientech Easy

One of the advantages of dynamic arrays is the ability to change their size at runtime. Here's how you can resize a 2D array:

```cpp void resizeArray(int**& arr, int oldRows, int oldCols, int newRows, int newCols) { // Allocate new memory for the resized array int** temp = new int*[newRows]; for(int i = 0; i < newRows; i++) { temp[i] = new int[newCols]; } // Copy the old data to the new array for(int i = 0; i < min(oldRows, newRows); i++) { for(int j = 0; j < min(oldCols, newCols); j++) { temp[i][j] = arr[i][j]; } } // Deallocate the old memory for(int i = 0; i < oldRows; i++) { delete[] arr[i]; } delete[] arr; // Update the pointer to the new array arr = temp; } ```

What is 2D Array In C#
What is 2D Array In C#
How to declare and Initialize two dimensional Array in Java with Example
How to declare and Initialize two dimensional Array in Java with Example
Search in a sorted 2d array
Search in a sorted 2d array
Array Data Structure - GeeksforGeeks
Array Data Structure - GeeksforGeeks
Multi-Dimensional Arrays in C (3D-2D-1D Arrays): Syntax, Examples & Real Use Cases (2026 Guide)
Multi-Dimensional Arrays in C (3D-2D-1D Arrays): Syntax, Examples & Real Use Cases (2026 Guide)
16 Repeated Addition Arrays Worksheets 2nd Grade
16 Repeated Addition Arrays Worksheets 2nd Grade
Arrays in C++ – Beginner Friendly Guide
Arrays in C++ – Beginner Friendly Guide
an image of a computer screen with the text, 2d array example sum of elements
an image of a computer screen with the text, 2d array example sum of elements
2D Array in Java
2D Array in Java
an array of different types of fruits on a yellow background with the words arrays
an array of different types of fruits on a yellow background with the words arrays
Arrays in C - GeeksforGeeks
Arrays in C - GeeksforGeeks
One Dimensional Array in Java - Scientech Easy
One Dimensional Array in Java - Scientech Easy
Two Dimensional Array in Java language - Codeforcoding
Two Dimensional Array in Java language - Codeforcoding
Learn Arrays in C++ in 10 minutes.
Learn Arrays in C++ in 10 minutes.
an image of a cube with numbers and times on the top side, in different colors
an image of a cube with numbers and times on the top side, in different colors
an abstract black and white image with circles, dots and lines in the shape of a tree
an abstract black and white image with circles, dots and lines in the shape of a tree
an image of a computer screen with the text traversing a 2d array on it
an image of a computer screen with the text traversing a 2d array on it
Array implementation
Array implementation
Java Multi-Dimensional Arrays
Java Multi-Dimensional Arrays

Syntax for Calling the Resize Function

```cpp int rows = 5, cols = 5; int** arr = new int*[rows]; // Initial allocation // ... resizeArray(arr, rows, cols, 10, 10); // Resize to 10x10 rows = 10, cols = 10; // Update the row and column counters ```

Remember to deallocate the memory when you're done using the array to prevent memory leaks. This is crucial when working with dynamic memory allocation in C++. In the world of data-driven applications, dynamic 2D arrays are essential tools for efficient storage and manipulation of multi-dimensional data. They allow for flexible, runtime-changing sizes, making them ideal for arrays that grow or shrink based on user input or dynamic changing requirements.