Skip to content
StudyFame

Pointer Arithmetic in C

If a pointer holds address 1000, what is p + 1? Most students say 1001. For an int pointer, the answer is usually 1004!

In this lesson you will learn pointer arithmetic: how pointers move with ++, --, + and -, how to subtract and compare pointers, and which operations are not allowed. This is the key to walking through arrays with pointers.

What You Will Learn

  • Why p + 1 moves by the size of the type.
  • Increment and decrement of pointers.
  • Adding or subtracting an integer.
  • Subtracting two pointers.
  • Comparing pointers.
  • Which pointer operations are invalid.

Pointer Arithmetic Explained

Exam definition: Pointer arithmetic is performing arithmetic on pointers. When an integer n is added to a pointer, the address increases by n × sizeof(type), so the pointer moves n elements, not n bytes. Allowed operations: pointer ± integer, ++, --, pointer − pointer (same array) and comparison.

Memory trick: A pointer counts in steps, not in bytes, like walking on stair steps: p + 1 means "next step", and the step size is the type size (1 for char, 4 for int, 8 for double).

int a[4] = {10, 20, 30, 40};  int *p = a;   (int = 4 bytes, a starts at 1000)

address :  1000   1004   1008   1012
value   :   10     20     30     40
pointer :   p     p+1    p+2    p+3

New address = old address + n x sizeof(int)
p + 2       = 1000 + 2 x 4 = 1008   ->  *(p + 2) = 30

Syntax of Pointer Operations

p++;          /* move to the next element     */
p--;          /* move to the previous element */
p = p + 3;    /* move 3 elements forward      */
q - p         /* number of elements between q and p (same array) */
p < q         /* is p before q? (same array)  */
OperationAllowed?Result
p + n, p - nYesA pointer n elements away
p++, p--, ++p, --pYesNext / previous element
q - pYes (same array)An integer: elements between them
p == q, p < q, …Yes (same array, or with NULL for ==)True / false
p + qNoCompile error: adding addresses is meaningless
p * 2, p / 2, p % 2NoCompile error

Think of house numbers: "house 40 minus house 32" (how far apart) makes sense. "House 40 plus house 32" makes no sense.

Example 1: How p + 1 Moves for Different Types

#include <stdio.h>

int main(void)
{
    char   c[3];
    int    i[3];
    double d[3];

    char   *pc = c;
    int    *pi = i;
    double *pd = d;

    /* bytes moved = (address after) - (address before), measured in bytes */
    printf("char*   p+1 moves %d byte(s)\n", (int)((char *)(pc + 1) - (char *)pc));
    printf("int*    p+1 moves %d byte(s)\n", (int)((char *)(pi + 1) - (char *)pi));
    printf("double* p+1 moves %d byte(s)\n", (int)((char *)(pd + 1) - (char *)pd));
    printf("int*    p+2 moves %d byte(s)\n", (int)((char *)(pi + 2) - (char *)pi));

    return 0;
}

Output

char*   p+1 moves 1 byte(s)
int*    p+1 moves 4 byte(s)
double* p+1 moves 8 byte(s)
int*    p+2 moves 8 byte(s)

Code Explanation

  1. Real addresses change every run, so instead of printing them we print how many bytes the pointer moved.
  2. Casting both addresses to char * and subtracting gives the distance in bytes (a char is 1 byte).
  3. pc + 1 moves 1 byte, pi + 1 moves 4 bytes, pd + 1 moves 8 bytes: always sizeof(type).
  4. pi + 2 moves 2 × 4 = 8 bytes: two int steps.

How Pointer Subtraction and Comparison Work

  • q - p gives the number of elements, not bytes. If p = &a[1] and q = &a[4], then q - p is 3.
  • The result type is ptrdiff_t (a signed integer). Print it with %td, or cast to int.
  • Comparison (<, >) tells which element comes first. It is meaningful only for pointers into the same array.
  • A pointer may point one past the last element (like a + n) for comparisons, but you must not dereference it.

Example 2: Walk an Array with p++ and Find an Element's Position

#include <stdio.h>

int main(void)
{
    int a[] = {15, 42, 8, 23, 4};
    int n = sizeof(a) / sizeof(a[0]);
    int *p, *start = a, *end = a + n;     /* end = one past the last */
    int *big = a;

    printf("Forward :");
    for (p = start; p < end; p++)          /* compare pointers */
    {
        printf(" %d", *p);
        if (*p > *big)
            big = p;
    }
    printf("\n");

    printf("Backward:");
    for (p = end - 1; p >= start; p--)
        printf(" %d", *p);
    printf("\n");

    printf("Largest %d is at index %d\n", *big, (int)(big - start));
    printf("Elements between first and last: %d\n", (int)((end - 1) - start));

    return 0;
}

Output

Forward : 15 42 8 23 4
Backward: 4 23 8 42 15
Largest 42 is at index 1
Elements between first and last: 4
  • p < end keeps the loop inside the array; p++ steps one int each time.
  • big - start is pointer subtraction: it gives the index of the largest element.
  • The backward loop starts at end - 1, the last real element.

Also see max and min element in an array using pointer.

*p++ vs (*p)++ vs ++*p

These look alike but do different things. ++ and * have the same precedence and group right to left.

ExpressionMeansChanges
*p++*(p++): use *p, then move pThe pointer
(*p)++Increase the value at p (after use)The value
++*p++(*p): increase the value, then use itThe value
*++pMove p first, then use the new *pThe pointer

Important Points to Remember

  • p + n moves n × sizeof(type) bytes.
  • Allowed: ± integer, ++/--, pointer − pointer, comparison.
  • Not allowed: pointer + pointer, multiply, divide, modulus.
  • q - p gives elements, not bytes.
  • Subtract and compare only pointers into the same array.
  • *p++ moves the pointer; (*p)++ changes the value.

Common Mistakes Beginners Make

MistakeWhat happensFix
Thinking p + 1 adds 1 byteWrong answers in address questionsIt adds sizeof(type)
p + qCompile errorUse q - p for a distance
Writing *p++ to increase the valuePointer moves instead(*p)++
Loop p <= a + nReads one past the endp < a + n
Subtracting pointers from different arraysUndefined behaviourOnly within one array

Pointer Arithmetic Practice Questions

  1. An int *p holds 2000 and int is 4 bytes. What is p + 3?
  2. A double *q holds 5000. What is q - 1?
  3. If p = &a[2] and q = &a[7], what is q - p?
  4. Why is adding two pointers not allowed?
  5. What is the difference between *p++ and (*p)++?
Show answers
  1. 2000 + 3 × 4 = 2012.
  2. 5000 − 8 = 4992.
  3. 5.
  4. The sum of two addresses doesn't point to anything meaningful.
  5. *p++ reads the value then moves the pointer; (*p)++ increases the value and leaves the pointer.

Coding Exercise

Using only a pointer (no a[i]), find the sum of the array {3, 6, 9, 12, 15} and add 1 to every element. Print the sum and the new array.

Show solution
#include <stdio.h>

int main(void)
{
    int a[] = {3, 6, 9, 12, 15};
    int *p, sum = 0;

    for (p = a; p < a + 5; p++)
    {
        sum += *p;
        (*p)++;                 /* increase the value, not the pointer */
    }
    printf("Sum = %d\nNew :", sum);
    for (p = a; p < a + 5; p++)
        printf(" %d", *p);
    printf("\n");
    return 0;
}
Sum = 45
New : 4 7 10 13 16

Quick Quiz: Pointer Arithmetic (MCQ)

  1. char *c holds 3000. c + 5 is:

    A) 3005   B) 3020   C) 3040   D) 3001

  2. Which operation is invalid?

    A) p + 2   B) p - q   C) p + q   D) p++

  3. q - p for two int pointers 3 elements apart gives:

    A) 12   B) 3   C) 4   D) 1

  4. int a[] = {5, 9}; int *p = a; int x = *p++; Now x and *p are:

    A) 5, 9   B) 6, 9   C) 9, 9   D) 5, 5

  5. int a[] = {5, 9}; int *p = a; (*p)++; Now a[0] is:

    A) 5   B) 6   C) 9   D) 10

Show answers
  1. A: 3005 (char is 1 byte).
  2. C: p + q.
  3. B: 3 elements.
  4. A: x = 5, then p points to 9.
  5. B: 6.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

p + nAddress + n × sizeof(type)
int* p + 1Moves 4 bytes (int = 4)
q − pNumber of elements between
Valid±int, ++, −−, ptr − ptr, compare
Invalidptr + ptr, ×, ÷, %
*p++Use value, then move pointer
(*p)++Increase value

Frequently asked questions

What happens when you increment a pointer?
It moves to the next element: the address grows by sizeof(type).
Can two pointers be added?
No. Only subtraction of two pointers (same array) is allowed.
What does subtracting two pointers give?
The number of elements between them, not bytes.

Remember

  • Pointers walk in steps, step size = type size.
  • House numbers: subtract yes, add no.

Next Topic

Next lesson: Pointers and Arrays in C. You will see why an array name behaves like a pointer and why a[i] equals *(a + i).