Pointer Arithmetic

This lesson highlights the different arithmetic operations we can perform on pointers.

Basic Addition and Subtraction

Consider a simple pointer, p, which points to a value of 10. What would happen if we increment it by 1?

Press + to interact
#include <iostream>
using namespace std;
int main() {
int *p = new int(10);
cout << "p: " << p << endl;
cout << "value of p: " << *p << endl << endl;
p = p + 1;
cout << "p: " << p << endl;
cout << "value of p: " << *p << endl << endl;
}

As we can observe, incrementing p actually increments its address. Since it is an integer pointer, the address jumps 4 bytes ahead (the size of an integer is 4 bytes).

The value of p becomes 0 which represents null or an empty ...