Dereferencing Pointers in C++

Dereferencing a pointer means accessing the value stored at the memory location to which the pointer points. This is done using the dereference operator *.

Example: Accessing Value Using a Pointer

#include <iostream>

int main() {
    int age = 40;
    int *ptr = &age;
    
    std::cout << "Value of age: " << *ptr << std::endl;
    return 0;
}

Explanation: The expression *ptr accesses the value at the memory address stored in ptr, which is age's value.

Key Takeaways

  • Dereferencing retrieves the value at the address stored in a pointer.
  • The * operator is used for dereferencing pointers.
  • Helps in indirect manipulation of variable values.