Menu
Coddy logo textTech

A Classic Example: Swap

Part of the Logic & Flow section of Coddy's C journey — lesson 28 of 63.

The swap function is one of the most famous examples that demonstrates why pointers are essential in C programming. The goal is simple: write a function that exchanges the values of two variables. However, this seemingly straightforward task reveals a fundamental limitation of pass-by-value.

Consider what happens if you try to swap two variables using regular pass-by-value:

void badSwap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    // This only swaps the copies, not the original variables!
}

This approach fails because the function only receives copies of the values. The original variables in the calling function remain unchanged.

The solution requires passing pointers to the variables instead:

void swap(int *a, int *b) {
    int temp = *a;  // Store the value at address a
    *a = *b;        // Put the value from address b into address a
    *b = temp;      // Put the stored value into address b
}

By passing the addresses of the variables and using the dereference operator, the function can directly modify the original variables' values. This makes the swap function a perfect demonstration of when and why you need pointers to accomplish tasks that pass-by-value simply cannot handle.

challenge icon

Challenge

Easy

Write a C program that implements and demonstrates the classic swap function using pointers. Your program should:

  1. Create a function named swap that takes two pointers to integers as parameters
  2. Inside the swap function, exchange the values at the memory addresses pointed to by the two pointers using a temporary variable
  3. In the main function, read two integers from the user input
  4. Print the original values in the format: Before swap: a = [value], b = [value]
  5. Call the swap function and pass the addresses of both variables using the address-of operator
  6. After the function call, print the swapped values in the format: After swap: a = [value], b = [value]

Your output should demonstrate that the function successfully exchanges the values of the original variables:

Before swap: a = [first_input], b = [second_input]
After swap: a = [second_input], b = [first_input]

This challenge demonstrates why pointers are essential for the swap operation - the function must access the original variables' memory locations to permanently exchange their values, which is impossible with pass-by-value.

Cheat sheet

The swap function demonstrates why pointers are essential in C. Pass-by-value cannot swap original variables because functions only receive copies:

void badSwap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    // This only swaps the copies, not the original variables!
}

The correct approach uses pointers to access and modify the original variables:

void swap(int *a, int *b) {
    int temp = *a;  // Store the value at address a
    *a = *b;        // Put the value from address b into address a
    *b = temp;      // Put the stored value into address b
}

By passing addresses and using the dereference operator (*), the function can directly modify the original variables' values.

Try it yourself

#include <stdio.h>

// TODO: Write your swap function here

int main() {
    int a, b;
    
    // Read input
    scanf("%d", &a);
    scanf("%d", &b);
    
    // Print original values
    printf("Before swap: a = %d, b = %d\n", a, b);
    
    // TODO: Call your swap function here
    
    // Print swapped values
    printf("After swap: a = %d, b = %d\n", a, b);
    
    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