Pointer Arithmetic in C

Pointer arithmetic in C is the process of performing arithmetic operations on pointers. This is possible because a pointer stores a memory address, and C allows certain controlled operations on that address. But pointer arithmetic does not behave like normal integer arithmetic. When you add 1 to a pointer, the address does not always increase by one byte. It increases by the size of the data type that the pointer points to.

This is one of the most important topics after learning basic pointers, because it explains how arrays, strings, and indexed memory access work internally in C. In this article, we will understand pointer arithmetic in C, valid operations, invalid operations, type-size stepping, array traversal, pointer subtraction, comparison, and common mistakes.

What is Pointer Arithmetic in C?

Pointer arithmetic in C means performing limited arithmetic operations on pointer variables. These operations let a pointer move across memory locations in units based on the pointer’s data type.

For example, if an int * pointer points to an integer and you add 1 to it, the pointer moves to the next integer location, not just the next byte.

In pointer arithmetic, adding or subtracting a number moves the pointer by that many elements, not by raw bytes in the way beginners often imagine.

Why Pointer Arithmetic is Needed in C

  • to move through array elements efficiently
  • to access sequential memory locations
  • to process strings and character arrays
  • to implement low-level memory-based logic
  • to understand how array indexing works internally

Without pointer arithmetic, many common C operations involving arrays and memory traversal would be much harder to express.

How Pointer Arithmetic Works in C

The key rule is simple:

  • adding 1 moves the pointer to the next element of its type
  • subtracting 1 moves the pointer to the previous element of its type
  • the actual byte movement depends on sizeof(type)
Pointer typeIf 1 is addedTypical movement
char *Moves to next character1 byte
int *Moves to next integertypically 4 bytes
float *Moves to next floattypically 4 bytes
double *Moves to next doubletypically 8 bytes

The exact byte size may depend on the system and compiler, but the idea remains the same: pointer arithmetic is scaled by the pointed data type.

Simple Example of Pointer Arithmetic in C

The following program shows how a pointer moves through integer memory locations.

#include <stdio.h>

int main(void)
{
    int arr[3] = {10, 20, 30};
    int *ptr = arr;

    printf("%d\n", *ptr);
    ptr = ptr + 1;
    printf("%d\n", *ptr);
    ptr = ptr + 1;
    printf("%d\n", *ptr);

    return 0;
}

Here, ptr first points to arr[0], then to arr[1], and then to arr[2]. The pointer moves by integer-sized steps.

Allowed Arithmetic Operations on Pointers in C

Not every arithmetic operation is allowed on pointers. The most common valid operations are:

  • increment: ptr++
  • decrement: ptr--
  • addition with integer: ptr + n
  • subtraction with integer: ptr - n
  • subtraction of two compatible pointers within the same array
  • comparison of compatible pointers
OperationValid?Meaning
ptr + 1YesMove to next element
ptr - 1YesMove to previous element
ptr++YesAdvance by one element
ptr1 - ptr2Yes, if relatedGet element distance
ptr1 + ptr2NoPointer addition is invalid
ptr * 2NoPointer multiplication is invalid

Increment and Decrement in Pointer Arithmetic

The increment and decrement operators are very common with pointers.

ptr++;
ptr--;

These move the pointer forward or backward by one element of its type. If ptr is an int *, then ptr++ moves it to the next integer location.

Pointer Arithmetic with Arrays in C

Pointer arithmetic is most commonly seen with arrays because array elements are stored in contiguous memory.

#include <stdio.h>

int main(void)
{
    int arr[5] = {1, 2, 3, 4, 5};
    int *ptr = arr;

    for (int i = 0; i < 5; i++)
    {
        printf("%d ", *(ptr + i));
    }

    return 0;
}

This program prints all array values using pointer arithmetic instead of normal indexing.

In fact, array indexing and pointer arithmetic are closely related in C:

  • arr[i] is equivalent to *(arr + i)
  • ptr[i] is equivalent to *(ptr + i)

Pointer Subtraction in C

Subtracting one pointer from another is allowed only when both pointers point into the same array or one past the same array. The result tells how many elements lie between them.

int arr[5] = {10, 20, 30, 40, 50};
int *p1 = &arr[4];
int *p2 = &arr[1];

printf("%td\n", p1 - p2);

The result is 3 because there are three integer positions between arr[1] and arr[4].

Pointer Comparison in C

Compatible pointers that refer to elements of the same array can also be compared using relational operators such as <, >, <=, and >=.

This is useful when iterating through memory ranges.

while (ptr < arr + 5)
{
    printf("%d ", *ptr);
    ptr++;
}

Invalid Pointer Arithmetic in C

Some pointer operations are invalid and should never be treated as legal C pointer arithmetic.

  • adding two pointers together
  • multiplying a pointer
  • dividing a pointer
  • subtracting unrelated pointers from different arrays or objects
  • moving far beyond valid object bounds and then dereferencing
Invalid expressionWhy it is invalid
ptr1 + ptr2C does not define pointer addition between two addresses
ptr * 2Multiplication is not a valid pointer arithmetic operation
ptr / 2Division is not defined for pointers
dereferencing unrelated moved addressCan produce undefined behavior

Pointer Arithmetic and Data Type Size

The data type of the pointer controls how far it moves in memory.

char *cptr;
int *iptr;
double *dptr;

If you add 1 to each of these pointers, they move by different byte distances because sizeof(char), sizeof(int), and sizeof(double) are different.

This is why the pointer type must always match the kind of data being accessed.

Advantages of Pointer Arithmetic in C

  • makes array traversal efficient
  • helps in low-level memory operations
  • supports string and buffer processing
  • reveals how indexed access works internally
  • is essential in some embedded and systems programming tasks

Common Mistakes in Pointer Arithmetic in C

  • thinking ptr + 1 always means one byte forward
  • dereferencing a pointer after moving outside valid bounds
  • subtracting pointers that do not belong to the same array
  • mixing the wrong pointer type with the wrong data
  • confusing pointer arithmetic with raw integer arithmetic
MistakeProblemBetter understanding
ptr + 1 means 1 byteWrong mental modelIt moves by one element of the pointed type
Moving beyond valid object rangeUndefined behavior riskStay within the same array or valid object range
Subtracting unrelated pointersInvalid resultOnly subtract compatible related pointers

Best Practices for Pointer Arithmetic in C

  • Use pointer arithmetic mainly where it improves clarity or performance intent.
  • Stay within array bounds and valid memory ranges.
  • Understand the pointed data type before moving the pointer.
  • Prefer clear code when normal indexing is easier to read.
  • Do not dereference pointers after invalid movement.

FAQs

What is pointer arithmetic in C?

Pointer arithmetic in C means adding, subtracting, incrementing, decrementing, or comparing pointers in valid ways based on the size of the pointed data type.

Why does adding 1 to a pointer not always move by 1 byte?

Because pointer arithmetic is scaled by the size of the pointed type. An int * moves by the size of an integer, a double * moves by the size of a double, and so on.

Can we add two pointers in C?

No. Adding two pointers is not a valid pointer arithmetic operation in C.

Can we subtract two pointers in C?

Yes, but only when both pointers refer to elements within the same array or related valid range.

What is the relation between array indexing and pointer arithmetic?

Array indexing is closely related to pointer arithmetic because arr[i] is equivalent to *(arr + i).

Is pointer arithmetic dangerous in C?

It can be dangerous if used carelessly, especially when pointers move outside valid memory bounds or are dereferenced incorrectly.