Apart from using analogies and diagrams to understand pointers like I have done with my other article on understanding pointers, the best way to get a grip of how they work is to actually delve into the code and play around with them.

In this practice assignment, I break down the code of creating pointers and printing out the pointer address variables and values. Pointers can be tricky to understand, but by having a strong baseline understanding of how they work can reduce confusion.

 

#include <iostream>

using namespace std;
 
int main()
{
  int* variable; // Pointer declared called variable
  int value = 10; // value = 10
 
  cout << value << "\n"; // This is going to print out the value of 10
 
  cout << &value << "\n"; // Memory address of variable value
 
  variable = &value; // This also is going to print out the memory address of the variable value
 
  cout << variable << "\n"; // Address of what variable has been pointed to
 
  cout << *variable; // Value of what has been pointed to
}
Share this post