Comparing Pointers
Part of the Logic & Flow section of Coddy's C journey — lesson 10 of 63.
You've learned how to move through arrays using pointer arithmetic. Now you'll discover that pointers can also be compared using the same relational operators you use with regular variables: ==, !=, <, and >.
When you compare two pointers, you're comparing their memory addresses. This is particularly useful when both pointers point to elements within the same array, as it allows you to determine their relative positions in memory.
int numbers[5] = {10, 20, 30, 40, 50};
int *start = numbers; // Points to first element
int *end = numbers + 4; // Points to last element
if (start < end) {
printf("start comes before end in memory");
}Pointer comparison becomes especially powerful in loop conditions. Instead of using a counter variable, you can compare your current pointer position with a target pointer to control when the loop should stop. This creates elegant, efficient code for array traversal.
The key insight is that ptr1 < ptr2 tells you that ptr1 points to an element that appears earlier in the array than the element ptr2 points to. This relationship makes pointer comparison a natural fit for controlling loops that process arrays sequentially.
Challenge
EasyWrite a C program that uses pointer comparison to traverse an array and find elements within a specific range. Your program should:
- Declare an integer array named
scoreswith 8 elements and initialize it with the values {45, 78, 92, 63, 87, 34, 91, 56} - Declare two pointers to integers:
start_ptrandend_ptr - Set
start_ptrto point to the first element of the array - Set
end_ptrto point to the last element of the array using pointer arithmetic - Declare a pointer named
current_ptrand initialize it to point to the first element - Use a while loop with pointer comparison (
current_ptr <= end_ptr) to traverse the array - Inside the loop:
- Check if the current value is between 60 and 90 (inclusive)
- If the value is in range, print "Value [value] is in range"
- If the value is not in range, print "Value [value] is out of range"
- Increment
current_ptrto move to the next element
- After the loop, use pointer comparison to verify that
current_ptris now beyond the end of the array by checking ifcurrent_ptr > end_ptr - If the condition is true, print "Traversal complete"
Your output should display the results in the following format:
Value 45 is out of range
Value 78 is in range
Value 92 is out of range
Value 63 is in range
Value 87 is in range
Value 34 is out of range
Value 91 is out of range
Value 56 is out of range
Traversal completeThis challenge tests your understanding of pointer comparison operators in loop conditions, using pointer arithmetic to set boundary pointers, and how pointer comparison can control array traversal more elegantly than traditional counter-based loops.
Cheat sheet
Pointers can be compared using relational operators: ==, !=, <, and >.
When comparing pointers, you're comparing their memory addresses. This is useful for determining relative positions of elements within the same array.
int numbers[5] = {10, 20, 30, 40, 50};
int *start = numbers; // Points to first element
int *end = numbers + 4; // Points to last element
if (start < end) {
printf("start comes before end in memory");
}Pointer comparison is particularly powerful in loop conditions, allowing you to control loops by comparing pointer positions instead of using counter variables:
int *current_ptr = numbers;
int *end_ptr = numbers + 4;
while (current_ptr <= end_ptr) {
// Process current element
current_ptr++; // Move to next element
}The expression ptr1 < ptr2 means ptr1 points to an element that appears earlier in the array than the element ptr2 points to.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code below
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