Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Function returns incorrect double value, debug shows value being returned is ok

double stack_pop_dbl(Stack *inStack)
{
double temp = inStack->dbl_stack[inStack->top];
inStack->dbl_stack[inStack->top] = 0.0;
inStack->top --;

return temp;
}

Then in main:
...
printf( "%lf\n", stack_pop_dbl(stackPtr) )
...

Now with my debugger, I checked the value of temp JUST BEFORE the return statement, and it's what I expect, then for some reason, when it returns to main it's become some random huge double :(

Note: This is all done in C and needs to remain so.

Could someone pls help?
[626 byte] By [Led] at [2007-11-11 8:45:34]
# 1 Re: Function returns incorrect double value, debug shows value being returned is ok
I think it might be worth noting that the INT version of this works fine.
Led at 2007-11-11 21:00:58 >
# 2 Re: Function returns incorrect double value, debug shows value being returned is ok
Check the printf flags. Are you sure that %lf is the right flag? Try %f instead.
Also, try to store the result in a temporay variable and print that variable:

double d= stack_pop_dbl(stackPtr)
printf( "%lf\n", d)
Danny at 2007-11-11 21:02:01 >
# 3 Re: Function returns incorrect double value, debug shows value being returned is ok
Just tried %f and the temp var, no luck still :(
Led at 2007-11-11 21:03:04 >
# 4 Re: Function returns incorrect double value, debug shows value being returned is ok
Found out another lil something, the calling function in this case is main as I wrote, the stack_pop_dbl method is in another file, now I made a method that just returned 32.2 inside the SAME file as main and it works...however if I make the same function in any other file and try to use it, I get the crazy huge values.
Led at 2007-11-11 21:04:10 >
# 5 Re: Function returns incorrect double value, debug shows value being returned is ok
Can't beleive that after 2 hours scouring through the .c and .h files I missed it, and I still don't know why it makes a difference, but turns out I had a slightly different name for stack_pop_dbl in the .h file (namely stack_pop_double) :( Sorry about that.

Still though, I made a new function, not defined in the .h file just to test if it could return 45.6 properly, why does it matter so much if it's defined in there? Is it because .h files are the ones you include?
Led at 2007-11-11 21:05:04 >
# 6 Re: Function returns incorrect double value, debug shows value being returned is ok
You shouldn't define functions inside a header file; only declarations of functions should be in header files. Make sure that the functions are defined in a .cpp file. The funtion declaration and its definition should match precisely: the name, the parameters, the return value etc. I short, it look slike you need to rebuild the entire project and make sure that your functions' declarations and definitions are in sync.
Danny at 2007-11-11 21:06:14 >