Introduction to C++ | Array of Pointers

C++ Array of Pointers

An array of pointers in C++ is an array where each element is a pointer. This type of array can store addresses of variables or objects, and it is often used to handle dynamic memory, manage arrays of strings, or store function pointers.

What is an Array of Pointers?

An array of pointers is simply an array where each element is a pointer to a variable or data type. Unlike a regular array that stores values, an array of pointers stores memory addresses. This allows you to work with arrays dynamically or to refer to multiple variables of different types.

Syntax:

Syntax of Array of Pointers


        dataType* arrayName[size];   // Array of pointers
                    

Example of Array of Pointers

Here’s an example of how to define and use an array of pointers to store the addresses of several integer variables:

Code Example: Array of Pointers


        #include <iostream>
        using namespace std;
        
        int main() {
            int a = 10, b = 20, c = 30;
            
            // Array of pointers to integers
            int* arr[3];
        
            // Assign addresses of variables to the array elements
            arr[0] = &a;
            arr[1] = &b;
            arr[2] = &c;
        
            // Accessing values through pointers
            cout << "Value at arr[0]: " << *arr[0] << endl;
            cout << "Value at arr[1]: " << *arr[1] << endl;
            cout << "Value at arr[2]: " << *arr[2] << endl;
        
            return 0;
        }
                    

Output

Value at arr[0]: 10
Value at arr[1]: 20
Value at arr[2]: 30

Array of Pointers with Dynamic Memory Allocation

An array of pointers is often used in dynamic memory allocation. You can use the new operator to allocate memory for each element in the array. This is useful when you don’t know the size of the data in advance and need to allocate memory during runtime.

Example of dynamic memory allocation with an array of pointers:

Code Example: Array of Pointers with Dynamic Allocation


        #include <iostream>
        using namespace std;
        
        int main() {
            // Array of pointers to integers
            int* arr[3];
            
            // Dynamically allocate memory for each integer
            for (int i = 0; i < 3; i++) {
                arr[i] = new int;  // Allocate memory for one integer
            }
        
            // Assign values to the dynamically allocated memory
            *arr[0] = 10;
            *arr[1] = 20;
            *arr[2] = 30;
        
            // Display the values
            cout << "Value at arr[0]: " << *arr[0] << endl;
            cout << "Value at arr[1]: " << *arr[1] << endl;
            cout << "Value at arr[2]: " << *arr[2] << endl;
        
            // Deallocate memory
            for (int i = 0; i < 3; i++) {
                delete arr[i];  // Free the dynamically allocated memory
            }
        
            return 0;
        }
                    

Output

Value at arr[0]: 10
Value at arr[1]: 20
Value at arr[2]: 30

Array of Pointers to Strings

An array of pointers is commonly used to store an array of strings (array of character arrays). Each pointer in the array points to the first character of each string. This is especially useful when working with multiple strings, as each string can have a different length.

Example of an array of pointers to strings:

Code Example: Array of Pointers to Strings


        #include <iostream>
        using namespace std;
        
        int main() {
            // Array of pointers to strings
            const char* arr[3] = {"Hello", "World", "C++"};
        
            // Displaying the strings
            for (int i = 0; i < 3; i++) {
                cout << "String at arr[" << i << "]: " << arr[i] << endl;
            }
        
            return 0;
        }
                    

Output

String at arr[0]: Hello
String at arr[1]: World
String at arr[2]: C++

Conclusion

An array of pointers is a versatile concept in C++ that can be used in many situations, such as handling dynamic memory, working with arrays of strings, and managing pointers to different data types. By using arrays of pointers, you can efficiently manage multiple variables or objects in memory.

Pro Tip:

💡 Pro Tip

When working with arrays of pointers, always ensure that each pointer is properly initialized, and that memory is properly allocated and freed when needed. Memory leaks can occur if dynamically allocated memory is not deallocated using delete.