Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?
double* weird( double a, double b )
{
double c;
c = pow( a + b, 0.5 );
return &c;
}
It might be trivial for most of you guys, but I don’t see what could go wrong here.
This is very wrong because you are returning a pointer to a local variable.
When
&cgets returned, the variablecwhose scope is the functionweirdwill be destroyed, thus giving you a pointer that points to a random area in your memory.This is undefined behaviour.