Pass by Reference Using Pointers
Understand how passing arguments by reference using pointers in C enables efficient data sharing between functions. Learn to pass memory addresses instead of copying values, making modifications in called functions that affect the caller. This lesson covers pointer syntax, advantages of pass by reference, and memory behavior to help you write better C code.
We'll cover the following...
Introduction
Pass by reference uses pointers to pass data from one function to another. Instead of sharing a variable by performing a copy, we can share its memory address with the caller function.
A pointer holds a memory address, so our arguments will be pointers when we want to implement pass by reference.
Pass by reference has the following advantages:
- Data is no longer copied, which saves execution time.
- The changes made by the callee are visible inside the called function. We say the changes made by the callee get propagated back to the caller. It happens because we’re no longer performing copies since we’re sharing the address of the actual variable and changing it through the pointer.
Motivating example
Let’s go back to one of our motivating examples.
The main ...