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++.

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.

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:

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

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

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

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; } ```



















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.