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].
1#include <iostream>2using namespace std;34int main() {5 int a = 10, b = 20, c = 30, d = 40, e = 50;67 // Array of pointers8 int* arr[5] = {&a, &b, &c, &d, &e};910 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 }1516 // Modify values through array of pointers17 *arr[0] = 100;18 *arr[1] = 200;1920 cout << "\nAfter modification:" << endl;21 cout << "a = " << a << ", b = " << b << endl;22 cout << "*arr[0] = " << *arr[0] << ", *arr[1] = " << *arr[1] << endl;2324 // Array of pointers to strings25 const char* names[] = {"Alice", "Bob", "Charlie", "David", "Eve"};2627 cout << "\nArray of pointers to strings:" << endl;28 for (int i = 0; i < 5; i++) {29 cout << "names[" << i << "] = " << names[i] << endl;30 }3132 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
Create five ints and an array of their addresses.
- 2
Print each address and *arr[i].
- 3
*arr[0] = 100 and *arr[1] = 200 change a and b.
- 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:
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:
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:
Array of Pointers to Strings
String Array:
const char* names[] = {"Alice", "Bob", "Charlie", "David", "Eve"};
What happens here:
Accessing Strings:
names[0] // Points to "Alice"
names[1] // Points to "Bob"
When to Use Array of Pointers
Best For:
Example Scenarios:
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:
Important Considerations
Memory Management:
String Literals:
Flexibility:
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.