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 arrayint arr[5];, the variable arr holds the memory address of arr[0].
c
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 + 1does NOT equal1001.
-
ptr + 1jumps ahead by one integer (4 bytes), so it equals1004.
c
5. Array Traversal using Pointers
You can traverse an array using pointers instead ofarr[i]. This is often faster at the assembly level.
c
6. Visualizing Pointer Arithmetic
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
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. Write a program to print elements of an array in reverse using a pointer.
- 2. Calculate the length of a string using pointer arithmetic (subtracting the end pointer from the start pointer).
- 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:
arr[i] equivalent to *(arr + i)?
a) Yes b) No
Answer: a) Yes
Question 4
Can you subtract two pointers?
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?
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
voidpointer? (No, because the compiler doesn't know the size of the data type).
12. Summary
Pointer arithmetic allows you to navigate memory dynamically. Adding1 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 usingmalloc and free.