I have a function to calculate square of a number. I expect output 9
when called sqr(3)
. But answer is wrong. The compiler gives warning about address of local variable returned
but I cannot decipher that warning. Another question in the declaration double* v1 = sqr(v2)
, why not double* v1 = &sqr(v2)
as one would do in case of other cases like int a=5; int *b=&a
?
#include <iostream>
double* sqr(double d)
{
double s = d*d;
return &s;
}
int main()
{
double v2 = 3.;
double* v1 = sqr(v2); //question why not double* v1 = &sqr(v2)
std::cout<<*v1;
}