Nearby lessons
56 of 72C - Passing Pointers to Functions
Learn how to pass a variable's address to a function so the function can change the original value.
Why Pass a Pointer to a Function?
When a normal value is passed to a function, the function receives a copy. If you pass the variable's address, the function can use a pointer to change the original variable.
Example01
Swap Two Numbers Using Pointers
This function swaps the original values because it receives their addresses.
Example02
Remember
- Use
&numberwhen calling the function to pass the address. - Use a pointer parameter such as
int *ptr. - Use
*ptrto access or change the original value.
In C, this is often called call by reference in beginner lessons, but technically the address is passed by value.
📝 Key Takeaways
- C passes function arguments by value.
- Passing an address lets a function access the original variable.
- Use * inside the function to change the value at that address.
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1