Creating array of pointers in C++
Last Updated :
23 Jun, 2022
An array of pointers is an array of pointer variables. It is also known as pointer arrays. We will discuss how to create a 1D and 2D array of pointers dynamically. The word dynamic signifies that the memory is allocated during the runtime, and it allocates memory in Heap Section. In a Stack, memory is limited but is depending upon which language/OS is used, the average size is 1MB.
Dynamic 1D Array in C++: An array of pointers is a type of array that consists of variables of the pointer type. It means that those variables can point to some other array elements.
Example:
int *p[3];
// Now P[0], P[1], P[2] can point to int memory blocks.
In a dynamically allocated array of size N, the block is created in the heap and returns the address of the first memory block. By using that address every element can be accessed. The dynamic array in C++ one should be familiar with the new keywords or malloc(), calloc() can be used.
Syntax:
<dataType> * <pointer name> = new <dataType> [<size>];
Example:
int *p = new int [5];

Accessing Elements of a Dynamic Array:
- 1. 1D array of size N(= 5) is created and the base address is assigned to the variable P. If the below statement is written then the output is 1000.
cout << p;
- If the value in the 1000th address is wanted then dereferenced it using the * (asterisk) symbol as illustrated below:
cout << *P;
// It is the same as P[0]. The output is 23.
Basic Pointer Arithmetic: Below is some points regarding Pointer Arithmetic:
P = 1000 and 1 = sizeof(int) = 4 bytes.
Hence, *(1004) and dereferencing by * (asterisk) symbol. Now, the final result is 38.
P = 1000
Hence, *(1000) and dereferencing by * (asterisk) symbol and then by adding 1 modifies the result to 23 + 1 = 24.
Below is the C++ program to illustrate the above concepts:
C++
// C++ program to illustrate the concepts
// of creating 1D array of pointers
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Dynamically creating the array
// of size = 5
int* p = new int[5];
// Initialize the array p[] as
// {10, 20, 30, 40, 50}
for (int i = 0; i < 5; i++) {
p[i] = 10 * (i + 1);
}
// Print the values using pointers
cout << *p << endl;
cout << *p + 1 << endl;
cout << *(p + 1) << endl;
cout << 2 [p] << endl;
cout << p[2] << endl;
*p++;
// Pointing to next location
cout << *p;
return 0;
}
Dynamic 2D Array of Pointers in C++: A dynamic array of pointers is basically an array of pointers where every array index points to a memory block. This represents a 2D view in our mind. But logically it is a continuous memory block.
Syntax:
<dataType> **<Pointer name> = new <dataType> *[<size>];
Example:
int **P = new int *[4];
Note: The *(asterisk) symbol defines the level of the pointer, one * means one level of pointers, where ** implies two levels of pointers, and so on. Also, the level of the pointer must be the same as the dimensional array you want to create dynamically.
Approach:
- Create a 1D array of pointers.

- Now, create the column as array of pointers for each row as:
- P[0] = new int [3];
- P[1] = new int [3];
- P[2] = new int [3];
- P[3] = new int [3];

- The 1D array of pointers are pointing to a memory block(size is mentioned). Basically, P[0], ..., P[3] are pointing to a 1D array of integers.
Accessing the array elements:
- *P is equal to P[0] which is the address of the 1st row, 1st column is &P[0][0] = 3000.
- *(P + 1) is equal to 'P' is 1000 + 1(sizeof int) = 1004 and * means dereferencing. So the value stored at the address is printed i.e., *1004 = 4000.
- *(P + 1) + 2 is same as above case but +2 means (&P[1] + 2) is equal to &P[1] [2] = 4008.
- *(*(P + 1) + 2) is same as above case but that first asterisk '*(....)' means dereferencing that address. Therefore, the result is equal to the value in &P[1][2] = *(4008) = 54.
Below is the C++ program to illustrate the above concepts:
C++
// C++ program to illustrate the concepts
// of creating 2-D array of pointers
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int N = 3;
// Creating the array of pointers
// of size N
int** p = new int*[N];
int x = 1;
// For multiplying
for (int i = 0; i < N; i++) {
p[i] = new int[N];
// Creating N sized int memory
// block
for (int j = 0; j < N; j++, x++) {
p[i][j] = 10 * x;
// The above statement can
// also be written as:
// *(*(p+i)+j) = 10 * x
}
}
// Print the values using pointers
cout << *p << endl;
cout << **p << endl;
cout << *p + 1 << endl;
cout << **p + 1 << endl;
cout << *(*(p + 1) + 0) << endl;
cout << p[2][2] << endl;
return 0;
}
Output0x158de90
10
0x158de94
11
40
90
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
12 min read
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C. Accessing the pointer directly will just give us the address that is stor
9 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read