Array of Pointers

Array of Pointers Program in C++

IntermediateTopic: Memory Management Programs
Back

C++ Array of Pointers Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20, c = 30, d = 40, e = 50;
    
    // Array of pointers
    int* arr[5] = {&a, &b, &c, &d, &e};
    
    cout << "Array of pointers:" << endl;
    for (int i = 0; i < 5; i++) {
        cout << "arr[" << i << "] = " << arr[i] 
             << " points to value: " << *arr[i] << endl;
    }
    
    // Modify values through array of pointers
    *arr[0] = 100;
    *arr[1] = 200;
    
    cout << "\nAfter modification:" << endl;
    cout << "a = " << a << ", b = " << b << endl;
    cout << "*arr[0] = " << *arr[0] << ", *arr[1] = " << *arr[1] << endl;
    
    // Array of pointers to strings
    const char* names[] = {"Alice", "Bob", "Charlie", "David", "Eve"};
    
    cout << "\nArray of pointers to strings:" << endl;
    for (int i = 0; i < 5; i++) {
        cout << "names[" << i << "] = " << names[i] << endl;
    }
    
    return 0;
}
Output
Array of pointers:
arr[0] = 0x7fff5fbff6ac points to value: 10
arr[1] = 0x7fff5fbff6a8 points to value: 20
arr[2] = 0x7fff5fbff6a4 points to value: 30
arr[3] = 0x7fff5fbff6a0 points to value: 40
arr[4] = 0x7fff5fbff69c points to value: 50

After modification:
a = 100, b = 200
*arr[0] = 100, *arr[1] = 200

Array of pointers to strings:
names[0] = Alice
names[1] = Bob
names[2] = Charlie
names[3] = David
names[4] = Eve

Understanding Array of Pointers

This program teaches you how to use Array of Pointers in C++. An array of pointers is an array where each element is a pointer. This enables storing addresses of different variables, creating arrays of strings, and implementing efficient data structures.

---

1. What This Program Does

The program demonstrates array of pointers:

Creating array where each element is a pointer
Storing addresses of different variables
Accessing values through array of pointers
Using array of pointers for strings

Array of pointers provides flexible and efficient data organization.

---

2. Header File Used

This header provides:

cout for displaying output
cin for taking input from the user

---

#include <iostream>

3. Understanding Array of Pointers

Array of Pointers Concept

:

Array where each element is a pointer
Each element stores an address
Can point to different variables
Enables indirect access through array

Declaration

:

dataType* arrayName[size];

Example

:

int* arr[5]; // Array of 5 integer pointers

---

4. Storing Addresses

Basic Usage

:

int a = 10, b = 20, c = 30, d = 40, e = 50;

int* arr[5] = {&a, &b, &c, &d, &e};

How it works

:

arr[0] stores address of a
arr[1] stores address of b
Each element points to different variable
Can access values through array

---

5. Accessing Values

Through Array

:

*arr[0] // Accesses value of a (10)

*arr[1] // Accesses value of b (20)

Modifying Values

:

*arr[0] = 100; // Changes a to 100

*arr[1] = 200; // Changes b to 200

How it works

:

arr[i] contains address
*arr[i] dereferences to get value
Modifications affect original variables

---

6. Array of Pointers to Strings

String Array

:

const char* names[] = {"Alice", "Bob", "Charlie", "David", "Eve"};

How it works

:

Each element is pointer to char array
String literals are char arrays
names[i] points to i-th string
Efficient string storage

Accessing Strings

:

names[0] // Points to "Alice"

names[1] // Points to "Bob"

---

7. When to Use Array of Pointers

Best For

:

Storing addresses of different variables
Arrays of strings
Dynamic memory allocation
Data structures (trees, graphs)
Variable-length strings

Example Scenarios

:

Command-line arguments (char* argv[])
Array of variable names
Dynamic string arrays
Tree node pointers

---

8. Dynamic Memory with Array of Pointers

Example

:

int* arr[5];

for (int i = 0; i < 5; i++) {

arr[i] = new int(i * 10);

}

How it works

:

Each pointer points to dynamically allocated memory
Can have different sizes
Requires individual delete for each

---

9. Important Considerations

Memory Management

:

If pointing to dynamic memory, delete each
for (int i = 0; i < 5; i++) delete arr[i];
Prevents memory leaks

String Literals

:

const char* for string literals
String literals are read-only
Don't modify through pointer

Flexibility

:

Can point to variables of same type
Enables indirect access
Useful for organizing data

---

10. return 0;

This ends the program successfully.

---

Summary

Array of pointers: array where each element is a pointer.
Stores addresses of different variables or strings.
Access values: *arr[i] dereferences pointer at index i.
Useful for: string arrays, dynamic memory, data structures.
Enables efficient organization and access of multiple variables.
Understanding array of pointers enables flexible data structures.
Essential for string manipulation and advanced memory management.

This program is fundamental for learning advanced pointer usage, understanding string arrays, and preparing for complex data structures and dynamic memory management in C++ programs.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Array of Pointers

This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents