Pointers

12.1 - Pointer intro


Pointers are objects which hold the address of another object.

Pointers are useful for holding a reference to another object, but we still want the reference to be optional.

Task

  • Read the code. Run the program and see the output.
    • Press the Run button.
#include <iostream> int main() { int n = 10; // Creates a pointer to int called p and sets the address to n's address using the address-of operator &. int *p = &n; // Gets the value pointed by p using the dereference operator *. std::cout << *p << std::endl; // Use the dereference operator again to change the value pointed by p to 20. *p = 20; // n is affected by *p = 20. std::cout << n << std::endl; }