int (*p) (int, int); // p is a function pointer which have two parameter of type int.
p = &func1;
We are making a variable which is storing the address of instructions and using this address we can get to those instruction and we can execute that function.
#include <stdio.h>
#include <stdlib.h>
int sum(int a, int b)
{
return a+b;
}
int main()
{
int (*fPtr) (int, int); // fPtr is going to point a function which returns int and takes two int.
fPtr = ∑
int d = (*fPtr)(4,6);
printf("The value of d is : %d",d);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int sum(int a, int b)
{
return a + b;
}
void greetHelloAndExecute(int (*fptr)(int, int))
{
printf("Hello user\n");
printf("The sum of 5 and 7 is %d\n", fptr(5, 7));
}
void greeGmAndExecute(int (*fptr)(int, int))
{
printf("Good moring user\n");
printf("The sum of 5 and 7 is %d\n", fptr(5, 7));
}
int main()
{
int (*ptr)(int, int);
ptr = sum;
greeGmAndExecute(ptr);
greetHelloAndExecute(ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int calAvg(int a, int b)
{
return ((a + b) / 2);
}
void greetGaAndExecute(int (*fptr)(int, int))
{
printf("Good Afternoon\n");
printf("The average of 10 and 4 is = %d\n", fptr(10, 4));
}
void greetGeAndExecute(int (*fptr)(int, int))
{
printf("Good Evening\n");
printf("The average of 10 and 4 is = %d\n", fptr(10, 4));
}
int main()
{
int (*avg)(int, int);
avg = calAvg;
greetGaAndExecute(avg);
greetGeAndExecute(avg);
return 0;
}