#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
/********** Addition ***************/
int add(int a, int b)
{
int c = a + b;
return c;
}
/********** Substraction ***************/
int sub(int a, int b)
{
int c = a - b;
return c;
}
/********** Multiplication ***************/
int mul(int a, int b)
{
int c = a * b;
return c;
}
/********** Division ***************/
int division(int a, int b)
{
int c = a / b;
return c;
}
int main()
{
//function pointer with two int as an argument and retuns int value
int (*ptr)(int,int);
int sum; //to get the sum value using function pointer
int num1 = 2, num2 = 1;
printf("Number1 = %d\nNumber2 = %d\n",num1,num2);
int val1 = 50; int val2 = 20; //used with arrary of function pointer
ptr = add; //Assign the function address to function pointer
sum = (*ptr)(num1,num2); //Calling the function using function pointer
printf("Addition using function pointer = %d\n",sum);
ptr = sub; //Assign the function address to function pointer
sum = (*ptr)(num1,num2); //Calling the function using function pointer
printf("Substraction using function pointer = %d\n",sum);
//Array of 4 function pointers accepting two int arguments and returns int
int (*aptr[4])(int,int) = {add, sub, mul, division};
//Calling the appropriate function using array of function pointer
int fptrResult = (aptr[sum])(val1,val2);
printf("Result = %d",fptrResult);
getch();
return 0;
}
0 comments :
Post a Comment