Definition: In C, the technique commonly called call by reference passes the address of a variable to a function using a pointer. The function can then access and modify the original variable through that address.
Definition: In C, the technique commonly called call by reference passes the address of a variable to a function using a pointer. The function can then access and modify the original variable through that address.
x is passed to a function, the function
can use a pointer to change the actual value stored in x.
Definition: Address-based parameter passing is used when a function needs to modify the original variable of the calling function or when working with large data without making a separate copy of the data.
swap() function can exchange the values of two original
variables by receiving their addresses.
Definition: A pointer is a variable that stores the memory address of another variable.
int *p;, p is a pointer to an integer.
Definition: The & operator returns
the memory address of a variable.
&x means the address of variable x.
Definition: The * operator is used with
a pointer to access the value stored at the address held by that pointer.
p stores the address of x, then
*p accesses the value of x.
The function receives the address of x and changes
the value stored at that address.
#include <stdio.h>
void change(int *p)
{
*p = 20; // modify the original variable
}
int main()
{
int x = 10;
change(&x); // pass address of x
printf("x = %d
", x);
return 0;
}
x = 20
x. Using
*p, it modified the value stored at that address.
| Call by Value | Call by Reference (Address-Based) |
|---|---|
| Value is passed. | Address is passed. |
| Function works with a copy. | Function can access the original variable through a pointer. |
| Original variable normally remains unchanged by parameter assignment. | Original variable can be modified. |
Example: change(x) |
Example: change(&x) |
A pointer parameter is used when a function needs access to the caller's variable through its address.
void change(int *p) declares p as a
pointer to an integer.
Write a C program to swap two numbers using a function and pointers.
The original values in main() should be changed.
Pass the addresses of the two variables to swap().
Use pointer dereferencing and a temporary variable to exchange
their values.
To modify the caller's variables, pass their addresses to the function. The pointer parameters can then access and update the original values.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a; // store the value at address a
*a = *b; // copy value at b into a
*b = temp; // place original a value into b
}
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swap: x = %d, y = %d
", x, y);
swap(&x, &y); // pass addresses of x and y
printf("After swap: x = %d, y = %d
", x, y);
return 0;
}
Enter two numbers: 10 20 Before swap: x = 10, y = 20 After swap: x = 20, y = 10
The function receives the addresses of x and
y. Therefore, changes made through *a
and *b affect the original variables.
| Symbol | Meaning | Example |
|---|---|---|
& |
Address of a variable | &x |
* |
Pointer declaration / dereference | int *p, *p |
* accesses the original value.& operator is used to obtain a variable's address.| Concept | Remember |
|---|---|
| Common name | Call by reference / address-based passing. |
| What is passed? | Address of the variable. |
| Used with | Pointers. |
| Can original change? | Yes, through the pointer. |
& operator?* dereference operator?Watch a beginner-friendly explanation of pointers and call by reference in C.
A short handwritten-style revision sheet for pointers and call by reference will be provided here.
Use the mind map for quick revision of address, pointer, dereference and original variable modification.