Menu
Coddy logo textTech

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 icon

Challenge

Easy

Write a C program that demonstrates pointer arithmetic by using pointer increment operations to traverse and process an array. Your program should:

  1. Declare an integer array named data with 5 elements and initialize it with the values {12, 24, 36, 48, 60}
  2. Declare a pointer to an integer named ptr and initialize it to point to the first element of the array
  3. Use a for loop that runs 5 times to traverse the entire array using only pointer arithmetic
  4. Inside the loop:
    • Print the current value that ptr points to using the dereference operator
    • Print the memory address stored in ptr using the %p format specifier
    • Increment the pointer using ptr++ to move to the next element
  5. After the loop, reset ptr to point back to the first element of the array
  6. Use pointer arithmetic to directly access and print the third element (index 2) using *(ptr + 2)
  7. 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: 60

This 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 position

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

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

All lessons in Logic & Flow