Array of Pointers
Array of Pointers Program in C++
C++ Array of Pointers Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
Array of pointers provides flexible and efficient data organization.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Array of Pointers
Array of Pointers Concept
:
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
:
---
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
:
---
6. Array of Pointers to Strings
String Array
:
const char* names[] = {"Alice", "Bob", "Charlie", "David", "Eve"};
How it works
:
Accessing Strings
:
names[0] // Points to "Alice"
names[1] // Points to "Bob"
---
7. When to Use Array of Pointers
Best For
:
Example Scenarios
:
---
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
:
---
9. Important Considerations
Memory Management
:
String Literals
:
Flexibility
:
---
10. return 0;
This ends the program successfully.
---
Summary
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.