Pointers

12.3 - Dereference operator


The dereference operator * is used to access the object pointed by a pointer.

int target = 20;
int *p = ⌖

// Prints out the value pointed by p which is 20.
std::cout << *p << std::endl;

Note that the asterisk * is also used to declare pointer types.

Task

  • Use the dereference operator to print out the value pointed by p.
    • *p to get the value pointed by p.
#include <iostream> #include <string> int main() { std::string s{"Indonesia"}; std::string *p{&s}; // Print out the value pointed by p after this line. }