#includefloat Plus (float a, float b) { return a+b; } float Minus (float a, float b) { return a-b; } //Direct solution float (*GetPtr1(const char opCode))(float, float){ if(opCode == '+') return &Plus; else return &Minus; // default if invalid operator was passed } // Solution using a typedef typedef float(*pt2Func)(float, float); pt2Func GetPtr2(const char opCode){ if(opCode == '+') return &Plus; else return &Minus; // default if invalid operator was passed } // Execute example code void Return_A_Function_Pointer(){ printf("Executing Return_A_Function_Pointer\n"); float (*pt2Function)(float, float) = NULL; pt2Function=GetPtr1('+'); // get function pointer from function ’GetPtr1’ printf("%f\n",(*pt2Function)(2, 4));// call function using the pointer pt2Function=GetPtr2('-'); // get function pointer from function ’GetPtr2’ printf("%f\n",(*pt2Function)(2, 4));// call function using the pointer } int main(){ Return_A_Function_Pointer(); getchar();getchar(); return 0; }
Sunday, August 28, 2011
How to Return a Function Pointer
লেবেলসমূহ:
How to Return a Function Pointer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment