Pointer to a Function in C Program.
Hi Everyone,
Today we will discuss about Pointer to a Function in C Program and in other word we can say uses of Function in Pointers. In my previous blogs we already discussed about Pointers and Functions so please go through that blog carefully before this topic. Previous topics like Pointers and Functions you can able to see it in my archive folder.
dheeraj60.blogspot.com
Let’s Discuss about Pointer to a function or Pointer uses in Function programs. Just like variable pointers can also be passed to a function. C program allows passing a pointer to a function. For doing this, we simply declare the function parameter as a pointer type. Let’s understand with example of passing pointer to a function in C.
#include <stdio.h>
#include <stdlib.h>
void increment(int *incr, int a)
{
*incr = *incr+a;
}
int main()
{
int salary=0, incentive=0;
printf(" Please enter the employee current salary:");
scanf("%d", &salary);
printf(" Please Enter the incentive:");
scanf("%d", &incentive);
increment(&salary, incentive);
printf("Final salary of Employee: %d", salary);
return 0;
}
Here we are passing a pointer to a function , we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable.
Note : If you write the same program without pointer, you would find that the increment amount will not reflect in the salary, this is because the change made by the function would be done to the local variables of the function. When we use pointers, the value is changed at the address of variable. That’s the reason in output we see the salary with increments added in final value as salary.
Please see the output of above program in compiler code block.
Let’s take an other example where the function, which can accept a pointer which can also accept an array in below program to calculate the average value of the numbers. Below is the Program.
#include<stdio.h>
#include<stdlib.h>
double Average(int *arr, int value);
int main () {
int value1[4] = {3000, 2000, 300, 700};
double avg;
avg = Average( value1, 4) ;
printf("Average value of the numberis: %f\n", avg );
return 0;
}
double Average(int *arr, int value) {
int i, sum = 0;
double avg;
for (i = 0; i < value; ++i) {
sum += arr[i];
}
avg = (double)sum / value;
return avg;
}
Here in above code we declare function Average . and we use int arr with 4 elements. And after that we pass pointer to the array as an argument by using avg = Average( value1, 4) then we print the output and calculate the average of the value by using function in a pointer with array value declared in the program.
Below is the output of above program in compiler code block.
Here you are able to see the output of average value in image .
Hope you are able to understand the usage of Pointers in Function , also if you have any confusion on program please refer my previous blogs of Functions and Pointers for your reference . Please read this blog carefully and try to write more programs in Function that can use pointer as well as array . If you have any doubts let me know. In next blog i will come up with another topics.
Thanks.
Comments
Post a Comment