C++ pointers and references explained: tutorial with examples and code

The power of C++ comes from references and pointers. It allows programmers to directly manage a memory — for best and efficient hardware performance. Nevertheless, it requires a very deep understanding of using it efficiently and in a way that will not create many bugs in the code.

This tutorial is an attempt to help programmers write better programs. Step-by-step comprehensive tutorial with many examples. However, even though these are fundamentals, yet very often misunderstood.

This is especially for people interested in learning C++, junior programmers or just for curious programmers from dynamic typed world.

The tutorial is divided into:

  1. Variables C++ introduction
  2. References introduction (&)
  3. Pointers introduction (*)
  4. References (&) vs pointers (*)
  5. C++ code

Variables C++ introduction

Let’s start with the simple C++ program that adds two numbers and print it to a console.

#include //function declaration
int add(int a, int b);
int main()
int a_main = 5;
int b_main = 10;