In C++, the dereference operator *
is used alongside a pointer ptr
to obtain the value of a variable.
Note: Learn what pointers are here.
#include <iostream>#include <string>using namespace std;int main() {// creating a variablestring name = "Theo";// declaring a pointerstring* ptr = &name;// obtaining the memory address of the variablecout << ptr << "\n";// Using the dereference operatorcout << *ptr << "\n";return 0;}
name
and assign Theo
as its value.ptr
which has the memory address of the variable.name
.