- Examples
Let's look at a couple of pointers examples.
We'll cover the following...
Basic pointers #
Press + to interact
#include <iostream>int main(){std::cout << std::endl;int i{2011};int* iptr= &i;std::cout << i << std::endl;std::cout << "iptr: " << iptr << std::endl;std::cout << "*iptr: " << *iptr << std::endl;std::cout << std::endl;int * jptr = iptr;*jptr = 2014;std::cout << "iptr: " << iptr << std::endl;std::cout << "*iptr: " << *iptr << std::endl;std::cout << "jptr: " << jptr << std::endl;std::cout << "*jptr: " << *jptr << std::endl;}
Explanation #
-
This example shows an instance of two pointers pointing to the same object.
-
Since both
iptr
andjptr
point toi
, changing the dereferenced value ofjptr
in line 18 changes the values ofi
andiptr
as well. ... ...