C Pointer Arithmetic

Intermediate
8 min read
Updated: Nov 2026
3 examples

What Is Pointer Arithmetic?

Pointer arithmetic lets you move a pointer through memory in units of the type it points to. Adding 1 to an int * advances it by sizeof(int) bytes, so the pointer lands on the next int. It is how functions like memchr() return a position inside a buffer.

Remember
p + n      pointer n elements after p
p - n      pointer n elements before p
p - q      number of elements between p and q   (type ptrdiff_t)
p++ / p--  step to the next / previous element
p < q      compare positions (same array only)

address(p + n) = address(p) + n * sizeof(*p)
OperationAllowed?Result
pointer + integerYesPointer, scaled by element size
pointer - integerYesPointer, scaled by element size
pointer - pointerYes (same array)ptrdiff_t element count
pointer + pointerNoCompile error
pointer * / /NoCompile error

How to Use Pointer Arithmetic in C

Point at an element of an array, then add or subtract integers while staying inside the array bounds.

  1. Point at an element: int *p = arr; (an array name decays to a pointer to its first element).
  2. Move with p + i, p++, or p -= 2; the compiler scales by sizeof(*p).
  3. Read or write through the pointer with *(p + i), which is the same as p[i].
  4. Subtract two pointers into the same array to get an element count or index.
  5. Compile with gcc file.c -std=c11 -o out and run.
Type of pp + 1 moves byNotes
char *1 byteUsed for raw byte offsets
int *sizeof(int) (usually 4)Next integer
double *sizeof(double) (usually 8)Next double
void *Not defined in standard CCast to unsigned char * first

Pseudocode

Pseudocode
p = address of arr[0]

q = p + 2            // address of arr[2]
value = *q           // read arr[2]
count = q - p        // 2 elements

stay within arr[0] .. arr[n]   // one past the end is the last legal address

Cheat Sheet

Keep these rules close when you walk memory with pointers.

TopicDetail
Scalingp + n adds n * sizeof(*p) bytes
Subscriptp[i] is exactly *(p + i)
Difference typeptrdiff_t from <stddef.h>; print with %td
Legal rangeFirst element up to one past the last element
Dereference limitNever dereference the one-past-the-end pointer
Different arraysSubtracting or comparing them is undefined behavior

Worked Walkthrough — Array of Four ints

Assume int arr[4] = {10, 20, 30, 40}; starts at address 1000 and sizeof(int) is 4.

Calculation
p = arr              -> address 1000   *p       = 10
p + 1                -> address 1004   *(p + 1) = 20
p + 2                -> address 1008   *(p + 2) = 30
p + 3                -> address 1012   *(p + 3) = 40
p + 4                -> address 1016   one past the end (compare only)

(p + 3) - p = 3 elements   (12 bytes apart, divided by sizeof(int))

C Examples

Compile with gcc file.c -std=c11 -o out. See also stddef.h for ptrdiff_t and string.h for pointer-returning functions.

Example 1 — Walk an Array with a Pointer

Add the loop counter to the base pointer to reach each element.

C
#include <stdio.h>
      
      int main(void) {
          int a[] = { 10, 20, 30, 40 };
          int *p = a;
          size_t n = sizeof a / sizeof a[0];
      
          for (size_t i = 0; i < n; i++) {
              printf("%d ", *(p + i));
          }
          printf("\n");
          return 0;
      }

How It Works

  1. Base pointer. p points at a[0].
  2. Element count. sizeof a / sizeof a[0] gives 4.
  3. Offset. p + i is the address of a[i].
  4. Dereference. *(p + i) reads the value.

Example 2 — Turn a memchr() Result into an Index

Library functions return pointers. Subtract the start pointer to get a zero-based index.

C
#include <stddef.h>
      #include <stdio.h>
      #include <string.h>
      
      int main(void) {
          const char *s = "hello, world";
          const char *comma = memchr(s, ',', strlen(s));
      
          if (comma != NULL) {
              ptrdiff_t idx = comma - s;
              printf("found ',' at index %td\n", idx);
          } else {
              printf("not found\n");
          }
          return 0;
      }

How It Works

  1. Search. memchr returns a pointer to the first match or NULL.
  2. Check for NULL. Never subtract from a null pointer.
  3. Subtract. comma - s counts char elements between them.
  4. Print with %td. That is the format for ptrdiff_t.

Example 3 — See the Scaling by Element Size

Pointer difference counts elements; casting to char * exposes the byte distance.

C
#include <stddef.h>
      #include <stdio.h>
      
      int main(void) {
          int arr[3] = { 1, 2, 3 };
          int *p = arr;
          int *q = p + 2;
      
          printf("q - p = %td elements\n", q - p);
          printf("byte distance = %td\n", (char *)q - (char *)p);
          return 0;
      }

How It Works

  1. Two pointers. q points two int elements past p.
  2. Element difference. q - p is 2, not 8.
  3. Byte difference. Casting both to char * makes each step 1 byte.
  4. Takeaway. The compiler divides or multiplies by sizeof(*p) for you.

Edge Cases & Pitfalls

Pointer arithmetic is easy to misuse. Check these cases.

bounds

Going out of bounds

Computing a pointer before the first element or beyond one-past-the-end is undefined behavior, even without dereferencing it.

end

Dereferencing one past the end

arr + n is a valid pointer for comparisons, but *(arr + n) reads memory you do not own.

arrays

Mixing different arrays

Subtracting or comparing pointers from different arrays has no defined meaning.

void

Arithmetic on void *

Standard C forbids it. GCC allows it as an extension (treating size as 1), which is not portable. Cast to unsigned char *.

format

Wrong printf format

Use %td for ptrdiff_t and %zu for size_t; %d can misprint on 64-bit builds.

Performance Notes

StyleCostNotes
p[i]O(1) per accessSame as *(p + i)
*p++ loopO(1) per stepOften the same machine code at -O2
Pointer differenceO(1)One subtraction and a divide by element size

Modern compilers generate equivalent code for index and pointer loops, so choose the form that reads best. Pointer arithmetic matters most when a library function hands you a position.

Key Takeaways

  • Scaling: p + n moves n elements, not n bytes.
  • Subscript: p[i] and *(p + i) are the same.
  • Difference: subtract pointers from one array to get an index, as ptrdiff_t.
  • Limits: stay between the first element and one past the last.

One line: pointer arithmetic counts in elements, and the compiler handles the byte math.

Frequently Asked Questions

It is adding or subtracting integers to a pointer, or subtracting two pointers, with results scaled by the size of the pointed-to type.
Pointer arithmetic is defined in elements. The compiler multiplies the integer by sizeof(*p) so p + 1 lands on the next element.
No. Adding pointers has no meaning and is a compile error. You can subtract two pointers into the same array.
ptrdiff_t, a signed integer type from <stddef.h>. Print it with the %td format.
Not in standard C. GCC accepts it as an extension, but portable code casts to unsigned char * or char * first.
Standard C allows a pointer to the element just after the last one, for comparisons and loop bounds. You must not dereference it.

Next: C Structures

Group related values and see how padding affects layout.

C Structures tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful