Hi,
again some problem,
In above program if I add scanf then its giving following warnings with -Wall options.
temp1.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
temp1.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’
temp1.c:24: warning: format ‘%c’ expects type ‘char *’, but argument 4 has type ‘int’
if I run the program then it ending in segmentation fault.
TIA.
Code:
1 #include<stdio.h>
2
3 float Plus (float a, float b) { return a+b; }
4 float Minus (float a, float b) { return a-b; }
5 /*
6 * Solution using a typedef:
7 * Define a pointer to a function which is taking
8 * two floats and returns a float
9 */
10 typedef float(*Op_fptr)(float, float);
11 Op_fptr GetPtr2(const char opCode)
12 {
13 if(opCode == '+')
14 return &Plus;
15 else
16 return &Minus;
17 }
18 int main()
19 {
20 int a=0,b=0;
21 char c = '+';
22 //float (*pt2Function)(float, float) = NULL;
23 Op_fptr pt2Function;
24 scanf("%d %d %c",a,b,c);
25 pt2Function=GetPtr2(c); // get function pointer from function 'GetPtr2'
26 printf("minus %f\n",(*pt2Function)(a, b)); // call function using the pointer
27 return 0;
28 }