Passing-Pointers-To-Function-In-C
INTRODUCTION
A reference in the programming language C represents a collection of methods containing instances.
Pointer to functions in C:
As another parameter, references could be provided for a method as well. Whenever we send a reference as a parameter in place inside a factor, the location of that object gets transmitted rather than its contents. The location associated with the given parameter becomes the eternal home for every modification the procedure makes regarding the reference. In C, it can be referred to as a call by reference.
Let us look at an example of this in C:
#include<stdio.h>
int add ();
int main ()
{
int res;
int (*ptr) ();
ptr = &add;
res = (*ptr) ();
printf ("The addition is %d", res);
}
int add ()
{
int b,s;
printf ("The numbers to be added are: ");
scanf ("%d %d", &b, &s);
return b+s;}
Output for the above code is as follows:
![PASSING POINTERS TO FUNCTION IN C](https://static.tutorialandexample.com/c/passing-pointers-to-function-in-c1.png)
Factors to Keep in Mind When Passing the Pointer:
- A method reference doesn't indicate information, instead serves as software.
- When utilizing method pointers, storage has not been generated or released.
- An operation's title may also be utilized to obtain its location.
- As regular references, you can have a collection of method references.
- One may utilize functional pointers to mark the location of the toggle scenario.
Let us look at another example of the passing parameter in C:
#include <stdio.h>
void salhi (int *var, int b)
{
*var = *var + b;
}
int main ()
{
int sal = 0, b = 0;
printf ("The current salary of an employee is:");
scanf ("%d", &sal);
printf ("The bonus to be included is:");
scanf ("%d", &b);
Delhi (&sal, b);
printf ("the final salary of the employee is: %d", sal);
return 0;
}
The output of the above code is as follows:
![PASSING POINTERS TO FUNCTION IN C](https://static.tutorialandexample.com/c/passing-pointers-to-function-in-c2.png)
Explanation
With this illustration, a procedure reference is passed. The location of a parameter must be given rather than its contents, while a reference is used as a parameter rather than a variable with a value. Therefore, any modification by an algorithm employing a reference is irrevocably performed based on the passed-in variable's location. In C, that technique is referred to as a call by reference.
If you run the identical program using the pointer, the bonus payment isn't going to appear in your pay. This is a consequence since each function's neighborhood variables were to be changed because of this modification. Once we employ suggestions, the quantity is altered at the variable's location.
Let us go with another example of swapping two numbers using passing parameters to function:
Among the most frequently used instances of call-by-reference number swapping was this one.
We could verify that the digits did not change when you ran this application with references. The cause is identical to what we observed with the initial case before.
#include <stdio.h>
void swap (int *n1, int *n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
int main( )
{
int b = 11, s = 77 ;
printf("The values before swapping are:");
printf("\nThe value of b is: %d", b);
printf("\nThe value of s is: %d",s);
/* swap function is called here */
swap(&b, &s);
printf("\nThe values after swapping are:");
printf("\nThe value of b is: %d", b);
printf("\nThe value of s is: %d", s);
}
The output of the above code is as follows:
![PASSING POINTERS TO FUNCTION IN C](https://static.tutorialandexample.com/c/passing-pointers-to-function-in-c3.png)
Let us have another example of this:
#include <stdio.h>
void addOne(int* ptr) {
(*ptr)++; // here we are adding one to the *ptr that is declared before
}
int main ()
{
int* s,j = 15;
s = &j;
addOne(s);
printf("The value of the pointer after the addOne function is %d", *s);
return 0;
}
The output of the above code is shown below:
![PASSING POINTERS TO FUNCTION IN C](https://static.tutorialandexample.com/c/passing-pointers-to-function-in-c4.png)
Explanation:
In this case, the quantity beginning with s, *s, was 15. The reference to s was subsequently given through the addOne() method. The addOne() method provides this location to the ptr value.
Employing (*ptr)++, we raised the value kept at ptr by one underneath the code. Because the addresses of ptr and s references are identical, the value of *s within main () additionally equals 16.