Introduction to C++ | Pointers

C++ Pointers

A pointer in C++ is a variable that stores the memory address of another variable. Instead of holding a data value directly, a pointer holds the location where the value is stored in memory. Pointers are powerful tools used for dynamic memory allocation, function arguments, and efficient array manipulation.

What is a Pointer?

A pointer is a variable that contains the memory address of another variable. It is used to indirectly access the data stored at that memory address. Pointers are essential for managing memory and creating more complex data structures, such as linked lists and trees.

Syntax for declaring a pointer:

Syntax of a Pointer


        dataType* pointerName;
                    

Example of Declaring and Using Pointers

Here’s an example of declaring a pointer, initializing it, and using it to access a variable’s value:

Code Example: Pointer Basics


        #include <iostream>
        using namespace std;
        
        int main() {
            int num = 10;
            int* ptr = #  // Pointer ptr holds the address of num
        
            // Access the value of num using the pointer
            cout << "Value of num: " << num << endl;  // Direct access
            cout << "Value using pointer: " << *ptr << endl;  // Access through pointer
        
            // Print the address stored in the pointer
            cout << "Address of num: " << ptr << endl;
        
            return 0;
        }
                    

Output

Value of num: 10
Value using pointer: 10
Address of num: 0x7ffeea0c1a4c

Dereferencing Pointers

You can access the value stored at the memory address a pointer is pointing to by using the dereference operator (*). This operator allows you to retrieve the value that the pointer refers to.

In the previous example, *ptr is used to access the value stored at the address stored in the pointer ptr.

Example of Dereferencing a Pointer

Code Example: Dereferencing a Pointer


        #include <iostream>
        using namespace std;
        
        int main() {
            int num = 20;
            int* ptr = #  // Pointer to the address of num
        
            // Dereferencing the pointer to get the value stored at the address
            cout << "Value of num: " << num << endl;
            cout << "Dereferenced value of ptr: " << *ptr << endl;
        
            return 0;
        }
                    

Output

Value of num: 20
Dereferenced value of ptr: 20

Pointer Arithmetic

C++ allows pointer arithmetic, where you can perform operations on pointers. This is commonly used when dealing with arrays, as the name of the array itself is a pointer to its first element. You can increment or decrement a pointer to access the next or previous element in an array.

Here’s an example of pointer arithmetic to access elements in an array:

Code Example: Pointer Arithmetic


        #include <iostream>
        using namespace std;
        
        int main() {
            int arr[] = {10, 20, 30, 40, 50};
            int* ptr = arr;  // Pointer to the first element of the array
        
            // Access array elements using pointer arithmetic
            cout << "First element: " << *ptr << endl;  // Access first element
            ptr++;  // Move the pointer to the next element
            cout << "Second element: " << *ptr << endl;  // Access second element
        
            return 0;
        }
                    

Output

First element: 10
Second element: 20

Pointers and Arrays

In C++, arrays and pointers are closely related. The name of an array is essentially a constant pointer to its first element. You can use pointers to navigate through arrays and manipulate their contents.

For example, arr[i] can be written as *(arr + i), where arr is a pointer to the first element of the array, and i is the index.

Example of Using Pointers with Arrays

Code Example: Pointers and Arrays


        #include <iostream>
        using namespace std;
        
        int main() {
            int arr[] = {10, 20, 30, 40, 50};
            int* ptr = arr;  // Pointer to the first element of the array
        
            // Using pointer to access array elements
            for (int i = 0; i < 5; i++) {
                cout << "Element " << i << ": " << *(ptr + i) << endl;
            }
        
            return 0;
        }
                    

Output

Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Conclusion

Pointers are an essential feature of C++ that provide greater control over memory and enable efficient code, especially in tasks like dynamic memory allocation, array manipulation, and passing large structures to functions. Understanding pointers is crucial for mastering C++ and optimizing your code.

Pro Tip:

💡 Pro Tip

When working with pointers, always ensure that they point to valid memory before dereferencing them. Dereferencing an uninitialized or null pointer can lead to undefined behavior and crashes. It’s good practice to initialize pointers to nullptr when declaring them.