Nearby lessons
97 of 124C - Passing Pointers to Functions
Learn how to pass pointers to functions in C — the function receives the address, so it can change the original variables. This is call by reference, the technique behind swap programs and array processing.
Why Pass a Pointer?
When you pass a normal variable, the function gets a copy (call by value) and cannot change the original. When you pass a pointer, the function receives the address and can modify the original variable directly.
In simple words: call by value sends a photocopy; passing a pointer sends the house address — the function can walk in and change things.
The Classic Swap Program
The swap program is the standard example. Without pointers, swap cannot change the original values — with pointers it can:
Example02
Passing an Array to a Function
The name of an array is the address of its first element, so you can pass the array name directly and walk through it with a pointer:
Example03
Common Mistakes
- Forgetting the & when calling —
swap(a, b)passes copies and nothing changes. - Missing * inside the function —
x = ychanges the addresses, not the values. - Declaring
int *p, q;— onlypis a pointer;qis a plain int.
📝 Key Takeaways
- Pass &var to give a function the address
- Inside the function use *param to reach the original
- An array name is already an address — pass it directly
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1