- Examples

Let's look at a couple of pointers examples.

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 and jptr point to i, changing the dereferenced value of jptr in line 18 changes the values of i and iptr as well. ... ...