Skip to main content
C Programming Basics
CHAPTER 13 Beginner

Pointer Arithmetic

Updated: May 17, 2026
5 min read

# CHAPTER 13

Pointer Arithmetic

1. Introduction

Did you know that in C, the name of an array is actually just a pointer to its first element? Because arrays are stored in contiguous memory, you can use basic math (addition and subtraction) on pointers to navigate through arrays rapidly. This is called Pointer Arithmetic.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain the relationship between arrays and pointers.
  • Perform pointer addition and subtraction.
  • Traverse arrays using pointer arithmetic instead of indices.
  • Understand how data types affect pointer math.

3. Arrays are Pointers!

When you declare an array int arr[5];, the variable arr holds the memory address of arr[0].
c
1234
int arr[5] = {10, 20, 30, 40, 50};

printf("Address of arr[0]: %p\n", &arr[0]);
printf("Value of arr:      %p\n", arr); // Identical to &arr[0]!

4. Pointer Addition and Subtraction

You can add or subtract integers from a pointer. However, the math scales based on the data type the pointer points to.

If an int is 4 bytes, and ptr points to address 1000:

  • ptr + 1 does NOT equal 1001.
  • ptr + 1 jumps ahead by one integer (4 bytes), so it equals 1004.

c
123456
int arr[3] = {10, 20, 30};
int *ptr = arr; // ptr points to arr[0]

printf("Value: %d\n", *ptr);       // Prints 10
printf("Value: %d\n", *(ptr + 1)); // Prints 20
printf("Value: %d\n", *(ptr + 2)); // Prints 30

5. Array Traversal using Pointers

You can traverse an array using pointers instead of arr[i]. This is often faster at the assembly level.
c
1234567891011121314
#include <stdio.h>

int main() {
    int arr[] = {100, 200, 300, 400};
    int *ptr = arr;
    int size = 4;

    for (int i = 0; i < size; i++) {
        // ptr++ moves the pointer to the next memory address
        printf("Element %d: %d\n", i, *ptr);
        ptr++; 
    }
    return 0;
}

6. Visualizing Pointer Arithmetic

123456789
int arr[3] = {10, 20, 30};
int *ptr = arr;

Memory Address:   [1000]   [1004]   [1008]
Data:             [ 10 ]   [ 20 ]   [ 30 ]

ptr       == 1000  => *ptr       == 10
ptr + 1   == 1004  => *(ptr + 1) == 20
ptr + 2   == 1008  => *(ptr + 2) == 30

7. Comparing Pointers

You can compare pointers using relational operators (<, >, ==). This is useful for checking if a pointer has reached the end of an array.
c
123456789
int arr[3] = {10, 20, 30};
int *ptr = &arr[0];
int *end = &arr[2];

while (ptr <= end) {
    printf("%d ", *ptr);
    ptr++;
}
// Outputs: 10 20 30

8. Common Mistakes

  • Writing *ptr++ vs (*ptr)++:
  • *ptr++ : Returns the value, THEN moves the pointer to the next address.
  • (*ptr)++ : Increments the VALUE at that memory address, the pointer does not move.
  • Out-of-bounds pointer math: C will happily let you do ptr + 100. If you dereference it, your program will likely crash.
  • Adding two pointers: You can subtract pointers (gives the distance between them), but adding two pointers (ptr1 + ptr2) is illegal and makes no logical sense.

9. Exercises

  1. 1. Write a program to print elements of an array in reverse using a pointer.
  1. 2. Calculate the length of a string using pointer arithmetic (subtracting the end pointer from the start pointer).
  1. 3. Write a function that accepts an array via a pointer and sums its elements.

10. MCQ Quiz with Answers

Question 1

If ptr holds address 2000, and points to an int (4 bytes), what is ptr + 1?

Question 2

The name of an array in C acts as:

Q3. Is arr[i] equivalent to *(arr + i)? a) Yes b) No Answer: a) Yes
Question 4

Can you subtract two pointers?

Q5. Can you add two pointers together (ptr1 + ptr2)? a) Yes b) No Answer: b) No
Question 6

What does *ptr++ do?

Question 7

If char *p = "Hello", what is *(p+1)?

Question 8

Why does pointer math scale by data type?

Question 9

Which comparison checks if ptr1 is before ptr2 in memory?

Q10. Can you use arr++ if arr is an array name? a) Yes b) No, an array name is a constant pointer Answer: b) No, an array name is a constant pointer

11. Interview Questions

  • Q: Explain why arr[2] and *(arr + 2) do the exact same thing in C.
  • Q: What is the difference between *ptr++ and (*ptr)++?
  • Q: Can you perform pointer arithmetic on a void pointer? (No, because the compiler doesn't know the size of the data type).

12. Summary

Pointer arithmetic allows you to navigate memory dynamically. Adding 1 to a pointer moves it to the next element, automatically scaling by the size of the data type. Because an array name is just a pointer to its first element, arrays and pointers in C are deeply interconnected.

13. Next Chapter Recommendation

In Chapter 14: Dynamic Memory Allocation, you will learn how to ask the operating system for exact amounts of memory while the program is running using malloc and free.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·