Search⌘ K
AI Features

Subtraction

Explore pointer subtraction in C by understanding how decrementing pointers and subtracting values affects memory addresses. Learn to calculate offsets by subtracting two pointers with the same data type, helping you grasp essential pointer arithmetic concepts.

Introduction

Decrementing and subtraction are very similar to incrementing and addition. The same rules apply:

  • Decrementing a pointer: address = address - sizeof(data type)
  • Subtracting value from a pointer: address = address - value * sizeof(data type)

Example code for decrementing

It’s mostly the same code from the previous lesson. We declare two variables a and b. Then, we start with a pointer to a and decrement it (line 14). By decrementing, the pointer points to variable b.

The code appears to ...