Pointer Arithmetic
Part of the Logic & Flow section of Coddy's C journey — lesson 9 of 63.
You've learned how to access array elements using pointer notation like *(array + i). Now you'll discover an even more powerful technique: pointer arithmetic, which allows you to move pointers through memory using simple increment and decrement operations.
When you increment a pointer with ++ or decrement it with --, the pointer doesn't just move by one byte. Instead, it moves by the size of the data type it points to. For an integer pointer, ptr++ moves forward by sizeof(int) bytes, landing exactly on the next integer in memory.
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers; // Points to first element (10)
ptr++; // Now points to second element (20)
ptr++; // Now points to third element (30)This automatic size calculation is what makes pointer arithmetic so powerful and convenient. You don't need to worry about byte calculations - C handles the memory addressing for you based on the pointer's data type.
Pointer arithmetic is particularly useful for traversing arrays in loops. Instead of using array indices, you can increment a pointer to move through each element sequentially, which often results in more efficient code.
Challenge
EasyWrite a C program that demonstrates pointer arithmetic by using pointer increment operations to traverse and process an array. Your program should:
- Declare an integer array named
datawith 5 elements and initialize it with the values {12, 24, 36, 48, 60} - Declare a pointer to an integer named
ptrand initialize it to point to the first element of the array - Use a for loop that runs 5 times to traverse the entire array using only pointer arithmetic
- Inside the loop:
- Print the current value that
ptrpoints to using the dereference operator - Print the memory address stored in
ptrusing the%pformat specifier - Increment the pointer using
ptr++to move to the next element
- Print the current value that
- After the loop, reset
ptrto point back to the first element of the array - Use pointer arithmetic to directly access and print the third element (index 2) using
*(ptr + 2) - Use pointer arithmetic to directly access and print the last element (index 4) using
*(ptr + 4)
Your output should display the results in the following format:
Value: 12, Address: [address]
Value: 24, Address: [address]
Value: 36, Address: [address]
Value: 48, Address: [address]
Value: 60, Address: [address]
Third element: 36
Last element: 60This challenge tests your understanding of how pointer increment operations automatically move by the size of the data type, and how you can use pointer arithmetic to access array elements at specific offsets from a base pointer position.
Cheat sheet
Pointer arithmetic allows you to move pointers through memory using increment and decrement operations. When you increment a pointer, it moves by the size of the data type it points to, not by one byte.
Basic pointer arithmetic operations:
int numbers[5] = {10, 20, 30, 40, 50};
int *ptr = numbers; // Points to first element (10)
ptr++; // Now points to second element (20)
ptr++; // Now points to third element (30)You can also use pointer arithmetic to access elements at specific offsets:
*(ptr + 2) // Access element at offset 2 from current positionPointer arithmetic is useful for traversing arrays in loops, often resulting in more efficient code than using array indices.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code here
// Declare and initialize the array 'data' with values {12, 24, 36, 48, 60}
// Declare pointer 'ptr' and initialize it to point to the first element
// Use a for loop with pointer arithmetic to traverse the array
// Print each value and address, then increment the pointer
// Reset the pointer and use pointer arithmetic to access specific elements
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions