Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Pointer Arithmetic in C++

Move through an array with pointer arithmetic in C++: ptr + i, ptr++, and ptr--. Learn why addresses jump by sizeof(int) and which positions are legal.

BeginnerMemory Management ProgramsExample 2 of 11
pointer-arithmetic.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int arr[] = {10, 20, 30, 40, 50};
6 int* ptr = arr; // Points to first element
7
8 cout << "Array elements using pointer arithmetic:" << endl;
9
10 // Access elements using pointer
11 for (int i = 0; i < 5; i++) {
12 cout << "arr[" << i << "] = " << *(ptr + i) << endl;
13 cout << "Address: " << (ptr + i) << endl;
14 }
15
16 cout << "\nIncrementing pointer:" << endl;
17 ptr = arr; // Reset to beginning
18 for (int i = 0; i < 5; i++) {
19 cout << "Value: " << *ptr << ", Address: " << ptr << endl;
20 ptr++; // Move to next element
21 }
22
23 cout << "\nDecrementing pointer:" << endl;
24 ptr--; // Now points to last element
25 for (int i = 0; i < 5; i++) {
26 cout << "Value: " << *ptr << ", Address: " << ptr << endl;
27 ptr--; // Move to previous element
28 }
29
30 return 0;
31}

Sample Output

arr[1] = 20

*(ptr + 1).

Value: 50, Address: …

Last element, reached by ptr++ or ptr--.

In this one

  • Why ptr + 1 moves by one element, not one byte
  • How arr[i] equals *(arr + i)
  • How ++ and -- walk a pointer through an array
  • Which pointer positions are valid

How it works

  1. 1

    ptr = arr points at arr[0].

  2. 2

    Loop 1 prints *(ptr + i) and its address.

  3. 3

    Loop 2 prints *ptr then ptr++ five times.

  4. 4

    ptr-- steps back to the last element; loop 3 walks backwards.

Edge cases

Reading past the end

After loop 2, print *ptr.

ptr points one past the last element; dereferencing it is undefined behavior.

Step before the start

The final ptr-- in loop 3.

It forms a pointer before arr[0], which the standard says is undefined, even though it is never dereferenced here.

What's going on

Pointer arithmetic means adding to or subtracting from a pointer to move between elements of an array. The surprise is the step size: ptr + 1 does not move one byte, it moves one element. For an int that is usually 4 bytes, which is why the addresses in the output go up by 4 (…6a0, …6a4, …6a8).

int* ptr = arr; works because an array name used in an expression turns into a pointer to its first element. So ptr points at arr[0].

First loop: *(ptr + i) reads element i without moving ptr. In fact arr[i] is defined as exactly *(arr + i); the bracket is just nicer syntax.

Second loop: ptr++ moves the pointer itself forward one element each pass. After five passes ptr points one past the last element. That position is allowed to exist, but reading *ptr there is undefined.

Third loop: ptr-- first steps back to the last element, then the loop prints and steps back again. On the final pass, ptr-- moves to one before arr[0]. Merely forming that pointer is undefined behavior in standard C++, even though nothing reads it and it usually causes no visible problem. A cleaner loop would stop before that last decrement.

Arithmetic only makes sense inside one array. Subtracting two pointers into the same array gives the number of elements between them. Comparing or subtracting pointers into different arrays is undefined.

The only header is <iostream>. It gives you cout for displaying output and cin for taking input from the user.

Understanding Pointer Arithmetic

Arithmetic Concept:

Can add/subtract integers to/from pointers
Address changes by data type size
ptr + 1 moves to next element (not next byte)
Respects data type sizes automatically

Key Rule:

ptr + n moves n elements forward
Address increases by n × sizeof(dataType)
Example: int* ptr + 1 increases by 4 bytes (if int is 4 bytes)

Adding to Pointers

Basic Addition:

int arr[] = {10, 20, 30, 40, 50};

int* ptr = arr; // Points to arr[0]

*(ptr + 1) // Accesses arr[1] (value 20)

*(ptr + 2) // Accesses arr[2] (value 30)

What happens here:

ptr + 1: address increases by sizeof(int)
For int (4 bytes): address increases by 4
Points to next element in array

Incrementing Pointers

Increment Operation:

ptr++; // Moves to next element

Example:

ptr = arr; // Points to arr[0]

ptr++; // Now points to arr[1]

ptr++; // Now points to arr[2]

What happens here:

ptr++ increments address by sizeof(dataType)
Equivalent to ptr = ptr + 1
Moves pointer to next element

Subtracting from Pointers

Basic Subtraction:

*(ptr - 1) // Accesses previous element

Decrement Operation:

ptr--; // Moves to previous element

What happens here:

ptr - 1: address decreases by sizeof(dataType)
ptr--: decrements address by sizeof(dataType)
Moves pointer to previous element

Pointer Arithmetic with Different Types

int (4 bytes):

int* ptr;

ptr + 1 // Increases by 4 bytes

char (1 byte):

char* ptr;

ptr + 1 // Increases by 1 byte

double (8 bytes):

double* ptr;

ptr + 1 // Increases by 8 bytes

Key Point: Arithmetic automatically accounts for type size.

Traversing Arrays

Using Pointer Arithmetic:

int arr[] = {10, 20, 30, 40, 50};

int* ptr = arr;

for (int i = 0; i < 5; i++) {

cout << *(ptr + i) << endl; // Access arr[i]

}

Using Increment:

ptr = arr;

for (int i = 0; i < 5; i++) {

cout << *ptr << endl;

ptr++; // Move to next element

}

When to Use Pointer Arithmetic

Best For:

Traversing arrays efficiently
Dynamic memory operations
String manipulation
Implementing data structures
Performance-critical code

Example Scenarios:

Iterating through arrays
Processing strings character by character
Dynamic array operations
Linked list traversal

Important Considerations

Array Bounds:

Be careful not to go beyond array bounds
ptr + 10 on array of size 5 is invalid
Can cause undefined behavior

Type Safety:

Pointer arithmetic respects type size
int* + 1 ≠ char* + 1 (different increments)
Compiler handles size automatically

Pointer Comparison:

Can compare pointers (ptr1 < ptr2)
Useful for checking array bounds
Only valid for pointers to same array

return 0;

This ends the program successfully.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

int a[] = {5,6,7}; int* p = a; what is *(p + 2)?

Pick an answer to check it.

Question 2

If p is an int* at 0x100, what is p + 3 (4-byte ints)?

Pick an answer to check it.

Questions people ask

Why do addresses differ by 4?

sizeof(int) is 4 on typical systems, and pointer arithmetic scales by the element size.

What about a double*?

ptr + 1 moves 8 bytes for a typical double.

Can I subtract pointers?

Yes, within one array: the result is the element distance, of type ptrdiff_t.