Skip to Content
Course content

52: Practice Exercise: Swapping Values with Pointers

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this exact mistake more times than I can count, even from students who think they've "got" pointers. They write a swap function, run the code, and then stare at the screen in total confusion because the variables in main haven't changed at all.

The Illusion of the Local Swap

Usually, the mistake looks something like this. The programmer creates a function that takes two integers, performs the classic "temporary variable" shuffle, and assumes the magic happened globally:

void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;
    swap(x, y); 
    // x is still 10, y is still 20. What happened?
    return 0;
}

Here is the reality: C is a pass-by-value language. When you call swap(x, y), the program doesn't hand the function the variables x and y themselves. Instead, it makes a brand new copy of their values and hands those copies to the function. Inside swap, the copies are indeed swapped, but as soon as the function returns, those copies are incinerated. The original x and y in main were never even touched.

Working with Addresses, Not Copies

If we want to actually change the values of variables owned by another function, we can't pass the values. We have to pass the addresses of those variables. This is where pointers stop being a theoretical headache and start becoming a practical tool.

Instead of asking the function to "swap these two numbers," we are asking it to "go to these two locations in memory and swap whatever you find there."

void swap(int *a, int *b) {
    int temp = *a; // Go to address a, grab the value, put it in temp
    *a = *b;       // Go to address b, grab value, write it into address a
    *b = temp;     // Take temp and write it into address b
}

int main() {
    int x = 10, y = 20;
    // We use the & operator to pass the memory addresses
    swap(&x, &y); 
    // Now x is 20, y is 10.
    return 0;
}

Notice the difference in the function signature: int *a tells C that a isn't an integer, but a pointer to one. Inside the function, we use the dereference operator (*) to reach through that pointer and manipulate the actual memory slot. It's a bit like the difference between giving someone a photo of your house (pass-by-value) and giving them the actual GPS coordinates and a key (pass-by-pointer).

  • &x: "Give me the address of x."
  • int *a: "I am expecting an address of an integer."
  • *a: "Go to the address stored in 'a' and interact with the value there."



📋 Practical Task

The Integer Pair Shuffler

Write a C program that implements a value-swapping utility. Your program must meet the following requirements:

  • Create a function named swap_integers that takes two int * (integer pointers) as arguments.
  • Inside main, initialize two integer variables, first and second, with different values (e.g., 42 and 99).
  • Print the values of first and second before the swap.
  • Call the swap_integers function, passing the addresses of your two variables.
  • Print the values again after the function call to prove that the memory was modified directly.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.