Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Array of Pointers in C++

Store addresses in an array of pointers in C++, change variables through them, and keep C-strings in const char* names[]. Also: int* p[5] vs int (*p)[5].

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}

Sample Output

a = 100, b = 200

Changed through *arr[0] and *arr[1].

names[2] = Charlie

A const char* printed as text.

In this one

  • How int* arr[5] declares five pointers
  • Why *arr[i] means *(arr[i])
  • How writing through an element changes the original variable
  • How const char* arrays hold string literals

How it works

  1. 1

    Create five ints and an array of their addresses.

  2. 2

    Print each address and *arr[i].

  3. 3

    *arr[0] = 100 and *arr[1] = 200 change a and b.

  4. 4

    Print a list of names from a const char* array.

Edge cases

Modifying a literal

Try names[0][0] = 'a'; after dropping const.

String literals are read-only; writing to one is undefined behavior and often crashes.

Pointer to a local that ended

Store the address of a variable from a finished block.

The pointer dangles. Reading through it is undefined.

What's going on

An array of pointers is an array whose elements are addresses. int* arr[5] = {&a, &b, &c, &d, &e}; holds five pointers, each aimed at a separate int variable. Read the declaration from the name outward: arr is an array of 5, of int*.

The first loop prints each stored address and the value behind it with *arr[i]. Brackets bind tighter than *, so *arr[i] means *(arr[i]): take element i, then follow it.

*arr[0] = 100; writes through the first pointer, so the variable a itself becomes 100. The second print block proves it: a = 100, b = 200.

Notice the addresses in the sample output go down by 4 (…6ac, …6a8, …). That only reflects how this compiler placed five separate variables on the stack. They are not an array, so there is no promised order or spacing. Your machine may show a different pattern.

The second half uses const char* names[], an array of pointers to string literals. Each element points at the first character of a different literal, and cout prints a const char* as text until the \0. The const matters: string literals are read-only, so trying names[0][0] = 'a'; is not allowed.

Do not confuse int* arr[5] (five pointers) with int (*arr)[5] (one pointer to an array of five ints). The parentheses change everything.

The only header is <iostream>. It gives you cout for displaying output and cin for taking input from the user.

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};

What happens here:

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

What happens here:

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"};

What happens here:

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);

}

What happens here:

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.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is int (*p)[5]?

Pick an answer to check it.

Question 2

After *arr[2] = 7, which variable changes?

Pick an answer to check it.

Questions people ask

Why would I use an array of pointers?

To refer to existing objects without copying them, or to keep a list of strings of different lengths.

How is this different from a 2D array?

Each pointer can aim anywhere and point to data of a different length; a 2D array is one block of fixed-size rows.

Can I use vector<int*>?

Yes, and vector<string> is easier for the names.