Array of Pointers

Array of Pointers Program in C++

C++Intermediate
C++
#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

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

#include <iostream>

This header provides:

  • cout for displaying output
  • cin for taking input from the user

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.