Topic 12: Pointers

Pointers are a powerful feature in C++ that allow you to manipulate memory directly. A pointer is a variable that holds the memory address of another variable.

Declaring and Initializing Pointers

To declare a pointer, specify the type of the variable it points to, followed by an asterisk (*) and the pointer name. You can initialize a pointer by assigning it the address of a variable using the address-of operator (&):

Example: Declaring and Initializing a Pointer
  • 
    #include 
    using namespace std;
    
    int main() {
        int var = 10;
        int *ptr = &var;
    
        cout << "Value of var: " << var << endl;
        cout << "Address of var: " << &var << endl;
        cout << "Value of ptr (address of var): " << ptr << endl;
        cout << "Value at the address stored in ptr: " << *ptr << endl;
    
        return 0;
    }
                                        

Pointer Operators

Pointers use two main operators:

  • Address-of operator (&): Returns the memory address of a variable.
  • Dereference operator (*): Returns the value stored at the memory address pointed to by the pointer.

Null Pointers

A pointer that is not assigned any memory address is called a null pointer. A null pointer points to nothing and can be created by assigning the constant NULL or nullptr to the pointer:

Example: Null Pointer
  • 
    #include 
    using namespace std;
    
    int main() {
        int *ptr = nullptr;
    
        if (ptr == nullptr) {
            cout << "Pointer is null" << endl;
        }
    
        return 0;
    }
                                        

Pointers and Arrays

Pointers can be used to access elements of an array. The name of an array acts as a pointer to its first element. You can use pointer arithmetic to traverse the array:

Example: Pointers and Arrays
  • 
    #include 
    using namespace std;
    
    int main() {
        int arr[5] = {1, 2, 3, 4, 5};
        int *ptr = arr;
    
        for (int i = 0; i < 5; ++i) {
            cout << "Element " << i << ": " << *(ptr + i) << endl;
        }
    
        return 0;
    }
                                        

Pointers to Pointers

C++ allows you to create pointers to pointers, which means a pointer that stores the address of another pointer:

Example: Pointers to Pointers
  • 
    #include 
    using namespace std;
    
    int main() {
        int var = 10;
        int *ptr = &var;
        int **ptr2 = &ptr;
    
        cout << "Value of var: " << var << endl;
        cout << "Address of var: " << &var << endl;
        cout << "Value of ptr: " << ptr << endl;
        cout << "Address of ptr: " << &ptr << endl;
        cout << "Value of ptr2: " << ptr2 << endl;
        cout << "Value at the address stored in ptr2: " << *ptr2 << endl;
        cout << "Value at the address stored in the pointer pointed to by ptr2: " << **ptr2 << endl;
    
        return 0;
    }
                                        

Function Pointers

Pointers can also be used to point to functions. Function pointers can be useful for implementing callback functions or for passing functions as arguments to other functions:

Example: Function Pointers
  • 
    #include 
    using namespace std;
    
    void displayMessage() {
        cout << "Hello, World!" << endl;
    }
    
    int main() {
        void (*funcPtr)() = displayMessage;
        funcPtr();
    
        return 0;
    }