Originally Posted by

**Fordy**
>>float* res; *res = num1 + num2;

For starters, you declare a pointer without assigning it then dereference it and try to change its value

Hey thank you Fordy.. I added float* res=new float; .. After doing this the code is working fine

However I have one doubt..What is the best way to delete the memory assigned to the pointers in the code below

Code:

#include <iostream>
float* Plus(float num1, float num2) {float* res=new float; *res = num1 + num2; return res;}
typedef float*(*pFunc)(float, float);
pFunc GetPointer(char c)
{
if(c == '+')
return &Plus;
else if(c == '-')
return &Plus;
else
return &Plus;
}
float Calc(float num1, float num2, float* (*pCalcMethod)(float, float))
{
float x=23;
std::cout<< "result inside Calc method" << *pCalcMethod(num1, num2) << std::endl;
return x;
}
int main()
{
float result = Calc(50, 25, &Plus);
std::cout << "Result: " << result << std::endl;
system("PAUSE");
return 0;
}

Also in the below code if I use resultF pointer just like res in the earlier code(ie without initializing)..but it does not throw any error..why..??!!

Code:

#include <iostream>
float Plus(float num1, float num2) {return num1 + num2;}
typedef float(*pFunc)(float, float);
pFunc GetPointer(char c)
{
if(c == '+')
return &Plus;
//else
// return &Multiply;
else if(c == '-')
return &Plus;
else
return &Plus;
}
float* Calc(float num1, float num2, char c)
{
float (*pFuncRes)(float, float)=GetPointer(c);
float *resultF;
*resultF=pFuncRes(num1, num2);
return resultF;
}
int main()
{
float* result = Calc(50, 25, '+');
std::cout << "Result: " << *result << std::endl;
system("PAUSE");
return 0;
}