passing pointers to functions
diagram of Normal variables are passed by value
When learning C programming, one of the most important concepts to master is pointers. Pointers allow functions to access and modify variables directly, rather than working with just copies. This is especially useful when dealing with large data structures, arrays, and dynamic memory.
In this article, we’ll explain passing pointers to functions, why it is used, and demonstrate it with practical code examples.
In C, when you pass a variable to a function, it is usually passed by value. That means the function works with a copy of the variable, and changes inside the function do not affect the original variable.
However, if you pass the address of a variable (using a pointer), the function can directly modify the original value stored in memory. This is called passing by reference (through pointers).
Here’s the general syntax:
void functionName(dataType *ptr) {
// use *ptr to access or modify value
}
And while calling the function:
functionName(&variable);
&variable
→ gets the address of the variable.
*ptr
→ accesses the value at that address.
A classic example is swapping numbers. Without pointers, swapping inside a function doesn’t affect the original variables.
#include <stdio.h>
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 10, y = 20;
swap(x, y);
printf("x = %d, y = %d\n", x, y); // Still 10, 20
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y); // Now 20, 10
return 0;
}
✅ Here, we passed addresses (&x
and &y
) to the function. Inside swap()
, we dereferenced (*a
, *b
) to modify the original variables.
In C, arrays are always passed as pointers to their first element. This is why functions can easily modify arrays.
#include <stdio.h>
void updateArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
arr[i] = arr[i] * 2;
}
}
int main() {
int nums[5] = {1, 2, 3, 4, 5};
updateArray(nums, 5);
printf("Updated array: ");
for(int i = 0; i < 5; i++) {
printf("%d ", nums[i]);
}
return 0;
}
✅ The array is modified directly because the function receives a pointer to the first element.
Pointers are also useful with structures, especially when passing large data.
#include <stdio.h>
struct Student {
char name[20];
int age;
};
void updateAge(struct Student *s, int newAge) {
s->age = newAge; // using arrow operator for pointer to struct
}
int main() {
struct Student st = {"Rahul", 20};
updateAge(&st, 25);
printf("Name: %s, Age: %d\n", st.name, st.age);
return 0;
}
✅ Passing a pointer to the structure avoids copying large amounts of data and allows direct modification.
Modify Original Data – Changes inside the function affect actual variables.
Efficient Memory Usage – Avoids copying large arrays or structures.
Dynamic Memory Handling – Functions can allocate or free memory.
Essential for Data Structures – Used in linked lists, trees, and graphs.
Normal variables are passed by value (copy).
Pointers allow pass by reference (original data modification).
Useful for arrays, structures, and dynamic memory.
Always remember to use *
for dereferencing and &
for passing addresses.
Passing pointers to functions is one of the most powerful features in C. It enables efficient memory management and allows functions to modify variables directly. Whether you’re swapping numbers, updating arrays, or handling complex data structures, mastering pointers is essential for becoming a proficient C programmer.