← Back to C Course | Chapter 7: Pointers | Lesson 3 of 9

C Pointer Arithmetic

Pointer arithmetic means moving a pointer along memory, like stepping from one house to the next on a street. Adding one moves to the next item of its type.
Syntax
c
pointer_name++;  // move to the next element
pointer_name--;  // move to the previous element
pointer_name + n;

Incrementing a Pointer

For example, if ptr points to an int (4 bytes on most systems) at address 1000, then ptr++ makes it point to address 1004, not 1001 -- the compiler automatically scales the increment by sizeof(the pointed-to type).

Example: Incrementing a Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = arr;
	ptr++;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Decrementing a Pointer

Just as with incrementing, ptr-- moves the pointer back by exactly one element's worth of bytes, which is why pointer arithmetic only makes sense within (or one past the end of) a single array -- there's no guarantee about what lies before or after it in memory.

Example: Decrementing a Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = &arr[2];
	ptr--;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Adding Integers to Pointers

ptr + n is equivalent to walking n elements forward from ptr's current position, and is the mechanism underlying the equivalence between arr[i] and *(arr + i) -- both compute the same address before dereferencing it.

Example: Adding Integers to Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = arr;
	// Print formatted text to the screen
	printf("%d", *(ptr + 2));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Subtracting Integers from Pointers

This works the same way as addition but in reverse, and is commonly used when iterating an array backward from a pointer that started at the last element, decrementing toward the first.

Example: Subtracting Integers from Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {10, 20, 30};
	int *ptr = &arr[2];
	// Print formatted text to the screen
	printf("%d", *(ptr - 1));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointer Subtraction

This is genuinely useful for computing distances -- for example, if you have a pointer to the start and end of a substring within a larger array, subtracting them tells you exactly how many characters lie between them.

Example: Pointer Subtraction

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[5] = {1, 2, 3, 4, 5};
	int *start = &arr[0];
	int *end = &arr[4];
	// Print formatted text to the screen
	printf("%ld", end - start);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming p + 1 moves one byte, when it moves by the size of the pointed type.
  2. Moving a pointer past the end of an array and dereferencing it.
  3. Subtracting pointers that do not belong to the same array.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.