Passing Pointers to Functions in C: A Comprehensive Guide
Discover how to pass pointers to functions in C programming, enhancing your ability to reference and manipulate variables directly. This guide also covers the different ways to call functions in C for effective programming practices.
Passing Pointers to Functions in C
In C programming, a pointer is a variable that stores the address of another variable, effectively acting as a reference to that variable. You can pass a pointer to a function just like any other argument.
Ways to Call Functions in C
A function in C can be invoked in two ways:
- Call by Value
- Call by Reference
To call a function by reference, you define it to accept pointers to variables from the calling function. The syntax is as follows:
Syntax
type function_name(type *var1, type *var2, ...)
When you call a function by reference, the pointers to the actual argument variables are passed instead of their values.
Advantages of Passing Pointers to Functions
Passing a pointer to a function has two main benefits:
- It overcomes the limitation of pass by value, allowing changes to the value within the called function to directly affect the variable at the address stored in the pointer. This enables manipulation of variables across different scopes.
- It also allows more than one value to be effectively returned, as you can return a pointer to an array or a struct variable.
Examples of Passing Pointers to Functions
Example 1: Basic Pointer Passing
Let's define a function add()
that takes the references of two variables. When we call this function, we pass the addresses of the actual arguments.
Code Example
#include <stdio.h>
/* function declaration */
int add(int *, int *);
int main(){
int a = 15, b = 25; // Changed values
int c = add(&a, &b);
printf("Addition: %d", c);
}
int add(int *x, int *y){
int z = *x + *y;
return z;
}
Output
Addition: 40
Example 2: Swap Values by Passing Pointers
One common application of passing pointers is to swap the values of two variables. The following function swaps the values of two variables whose references are received.
Code Example
#include <stdio.h>
int swap(int *x, int *y){
int z;
z = *x;
*x = *y;
*y = z;
}
int main (){
int a = 15; // Changed values
int b = 30; // Changed values
printf("Before swap, value of a: %d\n", a);
printf("Before swap, value of b: %d\n", b);
swap(&a, &b);
printf("After swap, value of a: %d\n", a);
printf("After swap, value of b: %d\n", b);
return 0;
}
Output
Before swap, value of a: 15
Before swap, value of b: 30
After swap, value of a: 30
After swap, value of b: 15
Example 3: Passing an Array Pointer to a Function
The name of an array acts as the address of its first element, functioning as a pointer. In the example below, we declare an uninitialized array and pass its pointer to a function along with an integer. The function fills the array with the square, cube, and square root of the passed integer.
Code Example
#include <stdio.h>
#include <math.h>
int arrfunction(int, float *);
int main(){
int x = 120; // Changed value
float arr[3];
arrfunction(x, arr);
printf("Square of %d: %f\n", x, arr[0]);
printf("Cube of %d: %f\n", x, arr[1]);
printf("Square root of %d: %f\n", x, arr[2]);
return 0;
}
int arrfunction(int x, float *arr){
arr[0] = pow(x, 2);
arr[1] = pow(x, 3);
arr[2] = pow(x, 0.5);
}
Output
Square of 120: 14400.000000
Cube of 120: 1728000.000000
Square root of 120: 10.954451
Example 4: Passing String Pointers to a Function
In this example, we pass two strings to the compare()
function. A string in C is an array of characters. The function checks the length of both strings.
Code Example
#include <stdio.h>
int compare(char *, char *);
int main(){
char str1[] = "DOG"; // Changed value
char str2[] = "CAT"; // Changed value
int ret = compare(str1, str2);
return 0;
}
int compare (char *x, char *y){
if (strlen(x) > strlen(y)){
printf("Length of Str1 is greater than or equal to the length of Str2");
}
else{
printf("Length of Str1 is less than the length of Str2");
}
}
Output
Length of Str1 is less than the length of Str2
Example 5: Passing Struct Pointer to a Function
In C, a structure is a heterogeneous data type containing elements of different data types. The following example shows how to pass a struct pointer to a function. Here, we declare a struct variable rectangle
in main()
and pass its address to the area()
function.
Code Example
#include <stdio.h>
#include <string.h>
struct rectangle{
float len, brd;
double area;
};
int area(struct rectangle *);
int main(){
struct rectangle s;
printf("Input length and breadth of a rectangle: ");
scanf("%f %f", &s.len, &s.brd);
area(&s);
return 0;
}
int area(struct rectangle *r){
r->area = (double)(r->len * r->brd);
printf("Length: %f \nBreadth: %f \nArea: %lf\n", r->len, r->brd, r->area);
return 0;
}
Output
Input length and breadth of a rectangle:
15.0 25.0
Length: 15.000000
Breadth: 25.000000
Area: 375.000000
The concept of passing a pointer to a function can be extended to include passing a union pointer, pointers to multi-dimensional arrays, and pointers to self-referential structures. These have significant applications in various areas, including complex data structures and hardware control programming.