Array of Pointers in C++

Array of Pointers Program in C++.

IntermediateMemory Management ProgramsExample 4 of 11
array-of-pointers.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a = 10, b = 20, c = 30, d = 40, e = 50;
6
7 // Array of pointers
8 int* arr[5] = {&a, &b, &c, &d, &e};
9
10 cout << "Array of pointers:" << endl;
11 for (int i = 0; i < 5; i++) {
12 cout << "arr[" << i << "] = " << arr[i]
13 << " points to value: " << *arr[i] << endl;
14 }
15
16 // Modify values through array of pointers
17 *arr[0] = 100;
18 *arr[1] = 200;
19
20 cout << "\nAfter modification:" << endl;
21 cout << "a = " << a << ", b = " << b << endl;
22 cout << "*arr[0] = " << *arr[0] << ", *arr[1] = " << *arr[1] << endl;
23
24 // Array of pointers to strings
25 const char* names[] = {"Alice", "Bob", "Charlie", "David", "Eve"};
26
27 cout << "\nArray of pointers to strings:" << endl;
28 for (int i = 0; i < 5; i++) {
29 cout << "names[" << i << "] = " << names[i] << endl;
30 }
31
32 return 0;
33}

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

What's going on

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.

Header File Used

This header provides:

cout for displaying output
cin for taking input from the user
1#include <iostream>

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

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

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

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"

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

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

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

return 0;

This ends the program successfully.