Pointer Basics

Pointers are a fundamental concept in C++ and they allow us to access memory directly. They can be a bit confusing at first, but once you understand the basics, they become an invaluable tool in your programming toolbox.

What is a Pointer?

A pointer is a variable that stores the memory address of another variable. In other words, it "points" to the location of another variable in memory. You can think of a pointer as an arrow that points to a specific memory location.

Let's use an analogy to help visualize this concept. Imagine you have several boxes lined up, each containing a piece of data (a number or a character). Each box has its own unique address, just like houses on a street. A pointer is like giving someone directions to one of these boxes by telling them the box's address.

Pointer declaration in C++

int* ptr;

This declares a pointer ptr that will point to an integer variable (int). The asterisk (*) before the variable name indicates that it's a pointer.

How to Use Pointers

To use pointers effectively, we need to understand two important operators: the address-of operator (&) and the dereference operator (*).

Address-of Operator (&)

Address-of Operator

int  number = 42;
int* ptr = &number;

In this code snippet, we create an integer variable number and assign it the value 42. We then create an integer pointer ptr and assign it the address of number. Now, our pointer ptr is pointing to the memory location where number is stored.

Dereference Operator (*)

The dereference operator (*) allows you to access or modify the value stored at the memory address pointed to by a pointer. For example:

Dereference Operator

#include <iostream>

int main() {
    int  number = 42;
    int* ptr = &number;

    std::cout << *ptr; // Output: 42
    return 0;
}

In this example, when we use *ptr, we are essentially saying, "give me the value stored at the address that ptr is pointing to." Since ptr is pointing to number, using *ptr gives us the value of number.

Combining these operators.

To access memory by using its pointer, you can use both the dereference operator and the address-of operator.

Modifying the value at a pointer

You can also modify the value stored at the memory location by using the dereference operator

#include <iostream>

int main() {
    int  number = 42;
    int* ptr = &number;

    *ptr = 10;

    std::cout << number; // Output: 10
    return 0;
}

In this case, we used *ptr to change the value of number indirectly through its memory address. We are not modifying ptr itself, rather the value it points to.

Conclusion

Pointers are a powerful feature in C++ that allows for direct manipulation of memory addresses. They can be tricky at first but understanding how pointers work will help you write more efficient and flexible programs. Remember these key concepts:

  • Pointers store memory addresses.
  • Use the address-of operator (&) to get a variable's memory address.
  • Use the dereference operator (*) to access or modify the value at a memory address.