In this post, we are going to learn about how to Create Pointer arrays in C++ dynamically. We will learn about how to declare an array of pointers. We will learn this for 1-D and 2-D arrays in C++. By the end of this article, you will be able to answer the below questions:
- How to declare 1-D pointer array in C++.
- How to declare 2-D pointer array in C++.
1. How to declare 1-D pointer array in C++
In this example, we are going to create an array of pointers. As you might know pointers are not same like normal C++ variables.Pointers are kind of special variables that points to the addresses in memory.so to create an pointer array we will need to use memory locations and to use it we need to allocate memory.
Syntax
<dataType> *<Pointer name> = new <dataType> [<size>];
Steps to declare 1D dynamic array in C++ (Below Code example)
- This line is creating an array of pointers. But its just declaration and all pointers are pointing to null means no memory.
- This line is allocating the memory on heap to created array of pointers. Once this memory is allocated now the pointers are ready to be used.
- This is another approach where declaration can be done with memory allocation in single line of code.
- we can assign the values to the pointer in the array. This is only possible after proper memory is allocated without this application will crash.
- Finally we are freeing the memory used by the array of pointers. This is important steps to avoid memory leak in your program.
C++ Program to declare 1D array
int main()
{
int *numptr[5];
numptr = new int [5];
int *numptr1 = new int [5];
for (int i = 0; i < 5; i++)
{
numptr[i] = i;
numptr1[i] = i;
}
for (int i = 0; i < 5; i++)
{
cout << "The Pointer array element"<< i << "is :"<< *numptr << endl;
*numptr++;
}
delete [] numptr;
delete [] numptr1;
return 0;
}
2. How to declare Dynamic 2-D pointer array in C++
In this example, we are going to create an 2-D array of pointers. A 2-D array os pointers is an array of pointers. We used array of twice so it means we will need two * signs. Yes, we need 2 * signs as you can see from the syntax below.
Syntax
** = new *[];
Steps to Declare Dynimally array in C++
First, we are creating a dynamic array of size 10 pointers to int.
int *numptr; numptr = new int[10];
Next, we are pointing each of the pointers which we created above to another array of size 10 of integer values.
for (int i = 0; i < 10; ++i) {
numptr[i] = new int[10];
}
Next is we will assign values to each of the elements of this 10×10 array of pointers. We will iterate over 2 loops, one is rows and another is columns.
for (int i = 0; i < 10; ++i) // for each row
{
for (int j = 0; j < 10; ++j) // for each column
{
numptr[i][j] = value;
value++;
}
}
Finally, we are Printing the values using these pointers
cout << *numptr << endl;
cout << **numptr << endl;
cout << numptr[2][2] << endl;
At last, we are deleting the memory from the heap so we don’t create a memory leak.
for (int i = 0; i < 10; i++)
{
delete[] numptr[i];
}
delete[] numptr;
C++ Program to declare 2D dynamic array
include <iostream>
using namespace std;
int main()
{
int **numptr;
numptr = new int*[10];
for (int i = 0; i < 10; ++i) {
numptr[i] = new int[10];
}
int value = 1;
for (int i = 0; i < 10; ++i) // for each row
{
for (int j = 0; j < 10; ++j) // for each column
{
numptr[i][j] = value;
value++;
}
}
cout << *numptr << endl;
cout << **numptr << endl;
cout << numptr[2][2] << endl;
// At last we are deleting the memory from heap so we dont create a memory leak.
for (int i = 0; i < 10; i++)
{
delete[] numptr[i];
}
delete[] numptr;
}