C For Pointer Simple Example Pass By Reference Using
In C programming, functions typically receive arguments by value, meaning they work on copies of the data. However, to allow a function to modify the original variables passed to it, C provides a mechanism known as "pass by reference" using pointers. In this article, you will learn how to implement pass by reference using pointers in C with practical examples.
Problem Statement
When you pass a variable to a function in C, by default, a copy of that variable's value is sent to the function. This is known as "pass by value." While this protects the original data from being accidentally altered, it also means that any modifications made to the parameter inside the function will not affect the original variable outside the function's scope. This limitation becomes problematic when you need a function to directly change the state of variables defined in the calling scope, such as swapping two numbers or updating a configuration structure.
Example (Pass by Value Limitation)
Consider a simple attempt to swap two numbers using a "pass by value" approach:
// Swap by Value (Problematic)
#include <stdio.h>
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside function: a = %d, b = %d\\n", a, b);
}
int main() {
int num1 = 10;
int num2 = 20;
printf("Before swapByValue: num1 = %d, num2 = %d\\n", num1, num2);
swapByValue(num1, num2);
printf("After swapByValue: num1 = %d, num2 = %d\\n", num1, num2);
return 0;
}
Sample output:
Before swapByValue: num1 = 10, num2 = 20
Inside function: a = 20, b = 10
After swapByValue: num1 = 10, num2 = 20
As you can see, despite the swapByValue function successfully swapping its local copies of a and b, the original num1 and num2 in main remain unchanged.
Background & Knowledge Prerequisites
To understand pass by reference with pointers, you should be familiar with:
- Variables and Data Types: Basic understanding of declaring and using variables.
- Functions: How to define, declare, and call functions.
- Memory Addresses: The concept that every variable occupies a unique location in memory, accessible via its address.
- Pointers: Basic knowledge of what a pointer is (a variable that stores a memory address), the
&(address-of) operator, and the*(dereference) operator.
Use Cases or Case Studies
Pass by reference using pointers is fundamental in C for several common scenarios:
- Swapping Values: When you need a function to swap the values of two variables from the calling scope.
- Modifying Multiple Variables: If a function needs to calculate and "return" multiple values, it can modify variables passed by reference.
- Large Data Structures: Passing large arrays or structures by reference (passing a pointer to them) is more efficient than passing them by value (copying the entire structure).
- Dynamic Memory Allocation: Functions like
mallocandcallocoften return pointers, and functions that manage dynamic memory (e.g., reallocating an array) often take pointers to pointers to modify the original pointer. - Input/Output Operations: Many standard library functions, especially for file I/O, use pointers to buffers or data structures to read into or write from specific memory locations.
Solution Approaches
Pass by Reference using Pointers
Using pointers allows a function to access and modify the actual memory locations of the variables passed to it, thereby achieving "pass by reference."
This approach involves:
- Declaring function parameters as pointers.
- Passing the memory addresses of variables (using the
&operator) when calling the function. - Using the dereference operator (
*) inside the function to access and modify the actual values at those memory addresses.
// Pass by Reference using Pointers (Swap Example)
#include <stdio.h>
// Function to swap two integers using pointers (pass by reference)
void swapByReference(int *ptrA, int *ptrB) {
// Step 1: Declare a temporary variable to hold one of the values.
// Use '*' to dereference ptrA, accessing the value it points to.
int temp = *ptrA;
// Step 2: Assign the value pointed to by ptrB to the location pointed to by ptrA.
*ptrA = *ptrB;
// Step 3: Assign the temporary value (original *ptrA) to the location pointed to by ptrB.
*ptrB = temp;
printf("Inside function: *ptrA = %d, *ptrB = %d\\n", *ptrA, *ptrB);
}
int main() {
int num1 = 10;
int num2 = 20;
printf("Before swapByReference: num1 = %d, num2 = %d\\n", num1, num2);
// Call swapByReference, passing the memory addresses of num1 and num2
// The '&' operator gets the address of the variable.
swapByReference(&num1, &num2);
printf("After swapByReference: num1 = %d, num2 = %d\\n", num1, num2);
return 0;
}
Sample output:
Before swapByReference: num1 = 10, num2 = 20
Inside function: *ptrA = 20, *ptrB = 10
After swapByReference: num1 = 20, num2 = 10
Stepwise Explanation:
- Function Signature (
void swapByReference(int *ptrA, int *ptrB)): The functionswapByReferenceis declared to accept two parameters,ptrAandptrB, both of typeint *(pointer to an integer). This indicates that the function expects memory addresses of integers, not integer values directly. - Function Call (
swapByReference(&num1, &num2);): Inmain, whenswapByReferenceis called, the&(address-of) operator is used withnum1andnum2. This passes the actual memory addresses ofnum1andnum2to the function, rather than their values. - Dereferencing Inside the Function (
*ptrA,*ptrB):
-
int temp = *ptrA;:*ptrA"dereferences"ptrA, meaning it accesses the *value stored at the memory address* thatptrAholds. This value (10) is copied intotemp. -
*ptrA = *ptrB;: The value at the address pointed to byptrB(20) is assigned to the memory location pointed to byptrA. This effectively changes the value ofnum1inmainto 20. -
*ptrB = temp;: The value stored intemp(10) is assigned to the memory location pointed to byptrB. This changes the value ofnum2inmainto 10.
- Persistent Changes: Because the function directly modified the contents of the memory locations (not just local copies), the changes to
num1andnum2persist even afterswapByReferencefinishes execution, as demonstrated by the finalprintfinmain.
Conclusion
Passing by reference using pointers is a powerful and essential technique in C programming. It overcomes the limitations of pass-by-value by allowing functions to directly modify variables in the calling scope. This method is crucial for tasks like swapping values, efficiently handling large data structures, and returning multiple results from a function. Understanding how to use the address-of operator (&) and the dereference operator (*) is key to mastering this concept.
Summary
- Pass by Value: Functions work on copies; original variables are unaffected.
- Pass by Reference: Functions work directly on original variables via their memory addresses.
- Pointers: Used to store memory addresses, enabling pass by reference.
-
&(Address-of Operator): Used to get the memory address of a variable (e.g.,&myVariable). -
*(Dereference Operator): Used to access the value stored at the memory address a pointer holds (e.g.,*myPointer). - Benefits: Allows functions to modify calling-scope variables, efficient for large data, enables "returning" multiple values.