Menu
Coddy logo textTech

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 icon

Challenge

Easy

Write a C program that uses pointer comparison to traverse an array and find elements within a specific range. Your program should:

  1. Declare an integer array named scores with 8 elements and initialize it with the values {45, 78, 92, 63, 87, 34, 91, 56}
  2. Declare two pointers to integers: start_ptr and end_ptr
  3. Set start_ptr to point to the first element of the array
  4. Set end_ptr to point to the last element of the array using pointer arithmetic
  5. Declare a pointer named current_ptr and initialize it to point to the first element
  6. Use a while loop with pointer comparison (current_ptr <= end_ptr) to traverse the array
  7. 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_ptr to move to the next element
  8. After the loop, use pointer comparison to verify that current_ptr is now beyond the end of the array by checking if current_ptr > end_ptr
  9. 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 complete

This 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow