What Are Pointers

beginner c++11 memory

Related: Why Use Pointers and What Are References

A pointer is a kind of variable in C++ similar to int or std::string however instead of holding a value like the number 42 or the sentence “She is playing the piano” a pointer’s value is instead the address of another variable in your program.

Addresses themselves seem a little funny when you look at them because they represent a piece of your computer’s memory (RAM), and your computer has a lot of memory! Addresses are really just very large numbers and when printed in C++ they look like 0x7ffeebcb96dc.

Pointers are used so much in C++ that there’s no less than three pieces of syntax for working with them, the ampersand (&), the asterisk(*), and the arrow(->):

#include <iostream>

using std::cout;

int main() {
  int answer = 42;
  int* theAddressOfAnswer = &answer;

  cout << "The answer is: " << answer << "\n";

  cout << "The address of answer is: "
       << theAddressOfAnswer << "\n";

  cout << "The answer (through a pointer) is: "
       << *theAddressOfAnswer << "\n";
}
The answer is: 42
The address of answer is: 0x7ffee5fa26cc
The answer (through a pointer) is: 42

Pointers Are Variables

Pointers are just like any other variable in C++. Pointer values can change, they are mutable. Pointers can also be const. const pointers can’t change what they point to.

#include <iostream>

using std::cout;

int main() {
  int mariasAge = 25;
  int answer = 42;

  int* pointer = &answer;
  int* const constPointer = &answer;

  cout << "The value at pointer is: " << *pointer << "\n";
  cout << "The value at constPointer is: "
       << *constPointer << "\n";

  pointer = &mariasAge;
  //constPointer = &mariasAge; // We can't do this

  cout << "The value at pointer is: " << *pointer << "\n";
  cout << "The value at constPointer is: "
       << *constPointer << "\n";
}
The value at pointer is: 42
The value at constPointer is: 42
The value at pointer is: 25
The value at constPointer is: 42

Pointers Can Change the Values of Other Variables

Because pointers point to other variables and those variables have values pointers can be used to change the values they point to. We do this with the second meaning of asterisk(*), “the value of what is being pointed to”.

#include <iostream>

using std::cout;

int main() {
  int mariasAge = 25;
  int* pointer = &mariasAge;

  cout << "Maria's age is: " << mariasAge << "\n";

  *pointer = 27;
  cout << "Maria's age is: " << mariasAge << "\n";
}
Maria's age is: 25
Maria's age is: 27

Pointers Can Point to Nothing

A pointer that points to nothing has the special value nullptr. Any pointer can have the value nullptr. A pointer that has the value nullptr is false while any other pointer value is true. This means you can use if to check if a pointer is nullptr. It is a runtime error if you try to read the value of a pointer that points to nullptr. Your program will crash.

#include <iostream>

using std::cout;

int main() {
  int answer = 42;
  int* pointer = nullptr;

  if (pointer) {
    cout << "Pointer points to value: " << *pointer << "\n";
  } else {
    cout << "Pointer points to nothing\n";
  }

  pointer = &answer;

  if (pointer) {
    cout << "Pointer points to value: " << *pointer << "\n";
  } else {
    cout << "Pointer points to nothing\n";
  }

  cout << "We're going to crash now." << std::endl;
  pointer = nullptr;
  cout << *pointer << "\n"; // BOOM!
}
Pointer points to nothing
Pointer points to value: 42
We're going to crash now.

Pointers Can Change What They Point To

Pointers can change what they point to during the course of your program. This is different than references which cannot. Here’s an example:

#include <iostream>

int main() {
  int answer = 42;
  int anotherInt = 7;

  int* pointer = &answer;
  std::cout << *pointer << "\n";

  pointer = &anotherInt;
  std::cout << *pointer << "\n";
}
42
7

If you’re wondering “why would I even want to use pointers” you should check out Why Use Pointers in C++.


For more C++ By Example, click here.