Course contentsShow
← Computer Science
Lesson 315 of 2,8728. Linked Data StructuresFree lesson

Pointer Arithmetic Basics

Understanding how pointer arithmetic works with addition and subtraction based on the pointed-to type's size.

What You'll Learn

You'll learn how adding or subtracting numbers from pointers doesn't just move one byte at a time—it moves by the size of the data type the pointer points to.

How Pointer Arithmetic Works

When you perform arithmetic on a pointer, the compiler automatically scales your addition or subtraction by the size of the pointed-to type. If you have a pointer to an int and you add 1 to it, the address doesn't increase by 1 byte—it increases by the size of an int (typically 4 bytes).

Think of it like walking along a street of houses. If someone says "move forward 3 houses," you don't measure 3 feet—you move to the third house ahead, regardless of how wide each house is. Pointers work the same way: ptr + 3 means "move forward 3 elements," not 3 bytes.

The Scaling Rule

  • ptr + 1 moves forward by sizeof(type) bytes
  • ptr - 1 moves backward by sizeof(type) bytes
  • ptr + n moves forward by n * sizeof(type) bytes

Example with Different Types

int nums[] = {10, 20, 30};
int *p = &nums[0];      // Points to first element
p = p + 1;              // Now points to nums[1] (address increased by 4 bytes)

char letters[] = {'a', 'b', 'c'};
char *c = &letters[0];  // Points to first element
c = c + 1;              // Now points to letters[1] (address increased by 1 byte)

Notice how adding 1 moved the int pointer by 4 bytes but the char pointer by only 1 byte. The compiler handles this automatically based on the pointer's type.

Key Takeaway: Pointer arithmetic automatically scales by the size of the pointed-to type, so ptr + n moves n elements forward, not n bytes—making array navigation natural and intuitive.