I wrote following program,
Its compiling without errors but giving wrong output.
output should be 2 but giving 0;
Can anybody tell me what is my mistake?
Thanx in avd.
Code:float Plus (float a, float b) { return a+b; } float Minus (float a, float b) { return a-b; } /* * Solution using a typedef: * Define a pointer to a function which is taking * two floats and returns a float */ typedef float(*Op_fptr)(float, float); /* * Function takes a char and returns a function pointer. <opCode> specifies which function to return */ Op_fptr GetPtr2(const char opCode) { if(opCode == '+') return &Plus; else return &Minus; // default if invalid operator was passed } int main() { /* float (*pt2Function)(float, float) = NULL; */ Op_fptr pt2Function; pt2Function=GetPtr2('-'); // get function pointer from function 'GetPtr2' printf("minus %d\n",(*pt2Function)(4, 2)); // call function using the pointer return 0; }



LinkBack URL
About LinkBacks



