Course Content
C++ Intermediate | Mobile-Friendly
C++ Intermediate | Mobile-Friendly
Deeping into Pointers
Let’s return to the previous code:
Here the variable pb
is a pointer. It contains the address to the variable b
. Use the pointer without an asterisk
to output the address the pointer contains and with an asterisk to get the value of the variable by address:
cout << pb << endl; // address of the variable b cout << *pb << endl; // the value of the b
The pointer always points to the address of the declared variable, and if the value of the variable changes, the address doesn’t:
int b = 2; int *pb = &b; cout << *pb << endl; b = 1; cout << *pb << endl
Thanks for your feedback!