A is a variable that stores the address of another variable. It acts as a reference to the original variable. A pointer can be passed to a function, just like any other argument is passed.
A function in C can be called in two ways −
To call a function by reference, you need to define it to receive the pointer to a in the calling function. Here is the syntax that you would use to call a function by reference −
type function_name(type *var1, type *var2, ...)
When a function is called by reference, the pointers of the actual argument variables are passed, instead of their values.
Advantages of Passing Pointers to Functions
Passing a pointer to a function has two advantages −
- It overcomes the limitation of pass by value. Changes to the value inside the called are done directly at the address stored in the pointer. Hence, we can manipulate the variables in one scope from another.
- It also overcomes the limitation of a function that it can return only one expression. By passing pointers, the effect of processing of a function takes place directly at the address. Secondly, more than one values can be returned if we return a pointer of an or .
In this chapter, we shall see how to −
- Pass pointers to int variables
- Pass pointers to array
- Pass pointer to structure
Example of Passing Pointers to Functions
Let us define a function add() that receives the references of two variables. When such a function is called, we pass the address of the actual argument. Let us call the add() function by reference from inside the .
#include <stdio.h> /* function declaration */ int add(int *, int *); int main(){ int a = 10, b = 20; int c = add(&a, &b); printf("Addition: %d", c); } int add(int *x, int *y){ int z = *x + *y; return z; }
Output
Addition: 30
Swap Values by Passing Pointers
One of the most cited applications of passing a pointer to a function is how we can swap the values of two variables.
The following function receives the reference of two variables whose values are to be swapped −
/* function definition to swap the values */ int swap(int *x, int *y){ int z; z = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = z; /* put z into y */ return 0; }
Example
The main() function has two variables a and b; their addresses are passed as arguments to the swap() function.
#include <stdio.h> int swap(int *x, int *y){ int z; z = *x; *x = *y; *y = z; } int main (){ /* local variable definition */ int a = 10; int b = 20; printf("Before swap, value of a: %dn", a); printf("Before swap, value of b: %dn", b); /* calling a function to swap the values */ swap(&a, &b); printf("After swap, value of a: %dn", a); printf("After swap, value of b: %dn", b); return 0; }
Output
When you execute this code, it will produce the following output −
Before swap, value of a: 10 Before swap, value of b: 20 After swap, value of a: 20 After swap, value of b: 10
Passing an Array Pointer to a Function
In C programming, the name of an array acts the address of the first element of the array; in other words, it becomes a pointer to the array.
Example
In this example, we declare an uninitialized array in main() and pass its pointer to a function along with an integer.
Inside the function, the array is filled with the square, cube and square root of the passed integer. The function returns the pointer of this array, using which the values are accessed and printed in the main() function.
#include <stdio.h> #include <math.h> int arrfunction(int, float *); int main(){ int x = 100; float arr[3]; arrfunction(x, arr); printf("Square of %d: %fn", x, arr[0]); printf("Cube of %d: %fn", x, arr[1]); printf("Square root of %d: %fn", 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
When you run this code, it will produce the following output −
Square of 100: 10000.000000 cube of 100: 1000000.000000 Square root of 100: 10.000000
Passing String Pointers to a Function
Let us have a look at another example, where we will pass string pointers to a function.
Example
In this program, two are passed to the compare() function. A string in C is an array of char data type. We use the to find the length of the string.
#include <stdio.h> int compare(char *, char *); int main(){ char str1[] = "BAT"; char str2[] = "BALL"; int ret = compare(str1, str2); return 0; } int compare (char *x, char *y){ int val; 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
When you run this code, it will produce the following output −
Length of Str1 is less than the length of Str2
Passing Struct Pointer to a Function
In C programming, a is a heterogenous data type containing elements of different . Let”s see how we can pass a struct pointer to a function.
Example
In this example, a struct variable rectangle is declared in main() and its address is passed to a user-defined function called area(). When called, the area() function is able to use the elements of the variable with the indirection operator “→“. It computes the result and assigns it to the area element r→area.
#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 n Breadth: %f n Area: %lfn", r->len, r->brd, r->area); return 0; }
Output
Run the code and check its output −
Input length and breadth of a rectangle 10.5 20.5 Length: 10.500000 Breadth: 20.500000 Area: 215.250000
The logical extension of the concept of passing a pointer to a function leads to passing a pointer, i.e., the pointer of a , passing the pointer of a , etc., all these have important uses in different application areas such as complex data structures, hardware control programming, etc.