Skip to content

Module 6 · Pointers and Memory — topic 2 of 5

Pointer Arithmetic and Arrays

Adding to a pointer moves it by whole elements, not bytes. Learn why a[i] means *(a + i), how to walk arrays with pointers, pointer subtraction and the limits of legal pointer arithmetic.

Beginner 15 min read·Lesson 23 of 37 in C Programming

Learning objectives

  • Explain that p + 1 advances by sizeof(*p) bytes
  • Use the equivalence a[i] == *(a + i)
  • Traverse arrays and strings with pointers

Definition

Pointer arithmetic adds or subtracts integers to pointers into an array, moving by multiples of the pointed-to type's size; subtracting two pointers into the same array yields the number of elements between them as a ptrdiff_t.

Introduction

If p points to an int at address 1000, p + 1 is address 1004 on a system with 4-byte ints, not 1001. The compiler scales the arithmetic by the element size so that p + 1 always means “the next element”.

This is exactly how array indexing works. In C, a[i] is defined as *(a + i): start at the array, move i elements, dereference.

Why it matters

Pointer arithmetic is how library functions like strlen and memcpy are written, and how you efficiently walk buffers.

Understanding it removes the mystery from arrays decaying to pointers and from the many “what does this print” interview questions about *p++.

Detailed explanation

In most expressions an array name decays to a pointer to its first element: int *p = arr; means p = &arr[0]. Exceptions: sizeof arr, &arr and string-literal initialisers.

p + n points n elements ahead; p - n points back. p++ moves to the next element. q - p gives the number of elements between two pointers into the same array.

Valid pointer values are addresses of array elements and the address one past the last element (which may be compared but not dereferenced). Moving outside that range is undefined behaviour even if you never dereference it.

*p++ parses as *(p++): use the current element, then advance the pointer. (*p)++ increments the element instead.

ExpressionEquivalentMeaning
arr[i]*(arr + i)element i
&arr[i]arr + iaddress of element i
*p++*(p++)read element, then advance p
(*p)++p[0]++increment the element
q - p—element distance (ptrdiff_t)

Syntax

C
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr; /* &arr[0] */
int third = *(p + 2); /* 30, same as arr[2] */
p++; /* now points to arr[1] */
int *end = arr + 5; /* one past the end */
ptrdiff_t n = end - arr; /* 5 */
  • Arithmetic is in elements, not bytes.
  • Only compare/subtract pointers into the same array.
  • end may be compared with p but must not be dereferenced.

Code examples

Indexing is pointer arithmetic

C
#include <stdio.h>
int main(void) {
int arr[4] = {10, 20, 30, 40};
int *p = arr;
printf("%d %d %d\n", arr[2], *(arr + 2), *(p + 2));
printf("%d\n", 2[arr]); /* legal but never write this */
printf("step = %td bytes\n", (char *)(p + 1) - (char *)p);
return 0;
}
Output
30 30 30
30
step = 4 bytes

Because a[i] is *(a + i) and addition commutes, 2[arr] is valid C — a curiosity, not a style.

Walking an array with a pointer

C
#include <stdio.h>
int main(void) {
int data[] = {3, 1, 4, 1, 5};
int *end = data + 5;
int sum = 0;
for (int *p = data; p < end; p++)
sum += *p;
printf("sum = %d\n", sum);
return 0;
}
Output
sum = 14

More examples

String functions written with pointers

C
#include <stdio.h>
#include <stddef.h>
size_t my_strlen(const char *s) {
const char *start = s;
while (*s) s++;
return (size_t)(s - start);
}
void my_strcpy(char *dst, const char *src) {
while ((*dst++ = *src++) != '\0')
;
}
int main(void) {
char buf[32];
my_strcpy(buf, "pointer magic");
printf("%s has %zu chars\n", buf, my_strlen(buf));
int v[] = {5, 6, 7};
int *p = v;
int a = *p++; /* a = 5, p -> v[1] */
int b = (*p)++; /* b = 6, v[1] becomes 7 */
printf("a=%d b=%d v={%d,%d,%d}\n", a, b, v[0], v[1], v[2]);
return 0;
}
Output
pointer magic has 13 chars
a=5 b=6 v={5,7,7}

Step-by-step explanation

  • while (*s) s++; advances until the character is '\0' (value 0, false).
  • s - start is the number of characters passed: the length.
  • In my_strcpy, *dst++ = *src++ copies one character and advances both pointers; the loop ends after copying the terminator.
  • *p++ reads v[0] (5) then moves p to v[1].
  • (*p)++ returns v[1]'s old value (6) and increments v[1] to 7.

Expected output

Running “String functions written with pointers” prints exactly:

TextOutput
pointer magic has 13 chars
a=5 b=6 v={5,7,7}

Practical applications

  • memcpy, memset and strlen in the C library use pointer loops.
  • Network parsers advance a pointer through a packet buffer field by field.
  • Image processing iterates over pixel buffers with pointers for speed.

Best practices

  • Prefer indexing for clarity; use pointers when they make the code simpler or faster.
  • Use the half-open range [begin, end) for pointer loops.
  • Use const pointers for data you only read.

Common mistakes

Treating arithmetic as bytes

Avoid

int *p = arr;
p = p + sizeof(int); /* skips 4 elements */

Prefer

int *p = arr;
p = p + 1; /* next element */

The compiler already scales by sizeof(int). Adding it again moves too far.

Dereferencing one past the end

Avoid

for (int *p = a; p <= a + n; p++) sum += *p;

Prefer

for (int *p = a; p < a + n; p++) sum += *p;

a + n is a valid address to compare with but not to read; <= reads beyond the array.

Important notes

  • Pointer subtraction yields ptrdiff_t (print with %td).
  • Arithmetic on void * is not allowed in standard C (GCC permits it as an extension); cast to char * for byte offsets.

Key takeaways

  • p + n moves n elements; a[i] is *(a + i).
  • Array names decay to pointers to their first element.
  • Stay within the array and its one-past-the-end address.

Try it yourself

  1. 1
    Reverse an array in place using two pointers that move toward each other.
  2. 2
    Write count_char(const char *s, char c) using only pointer arithmetic (no indexing).

Finished reading? Mark it complete to update your dashboard.

Suggested practice

7 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • ModerateIf `int *p` holds address 1000 and int is 4 bytes, what address does `p + 1` hold?
  • Moderate`a[i]` is defined in C as equivalent to…
Practise this topic