C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
52: Practice Exercise: Swapping Values with Pointers
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_integersthat takes twoint *(integer pointers) as arguments. - Inside
main, initialize two integer variables,firstandsecond, with different values (e.g., 42 and 99). - Print the values of
firstandsecondbefore the swap. - Call the
swap_integersfunction, passing the addresses of your two variables. - Print the values again after the function call to prove that the memory was modified directly.
There are no comments for now.