Getting wrong answer while returning address (but right if I return value)

Reply

Join Date: Apr 2009
Posts: 6
Reputation: hypernova is an unknown quantity at this point 
Solved Threads: 0
hypernova hypernova is offline Offline
Newbie Poster

Getting wrong answer while returning address (but right if I return value)

 
0
  #1
Jun 6th, 2009

Hello everyone !
I am still a beginner, and am posting the code in which I have problems -
I compiled it in VC++ 6.0 standard edition, and it gave two warnings and no errors. The warnings disappear if i use double everywhere instead of float.
the warning (same twice)-
warning C4305: '=' : truncation from 'const double ' to 'float '
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. float fun1(float,float);
  5. float *fun2(float,float);
  6. int *fun3(int,int);
  7.  
  8. void main()
  9. {
  10. float i,j,k,prod,*l;
  11. int o=3,m=2,*n;
  12. i=7.56;
  13. j=3.39;
  14. k=fun1(i,j);
  15. l=fun2(i,j);
  16. n=fun3(o,m);
  17.  
  18. prod=i*j;
  19. printf("the product =\n\n%f",prod);
  20.  
  21.  
  22. printf("\n\nthe product(from function -call by value-return value) = %f",k);
  23.  
  24. printf("\n\nThe product (from function -call by value-return pointer)= %f",*l);
  25.  
  26. printf("\n\nThe product of integers -return pointer= %d",*n);
  27.  
  28.  
  29.  
  30.  
  31. }
  32. float fun1(float i,float j)
  33. {
  34. float prod;
  35. prod=i*j;
  36. return prod;
  37.  
  38. }
  39. float *fun2(float i, float j)
  40. {
  41. float *p,prod;
  42.  
  43. prod=i*j;
  44. p=&prod;
  45. return p;
  46.  
  47. }
  48. int *fun3(int i,int j)
  49. {
  50. int prod,*p;
  51. prod=i*j;
  52. p=&prod;
  53. return p;
  54.  
  55.  
  56. }

I have three questions -
1. If i use float instead of double, i get the answer 25.628401 (which is wrong(-ish), the answer is 25.628400)
2. the first two printf's give the right result, but the last two printf's give the wrong result. the second printf gives 0.000000 and the last printf gives a garbage six digit value.
3. if i replace "return p" by "return &prod" in fun2, VC++ gives a warning
warning C4172: returning address of local variable or temporary
I understand that after control in fun2 or fun3 ends, the local variables "die" and if i use static, i get the right answer, but why is this happening ?
When a variable is declared, a memory "cell" is allocated to it and the name (say i) is given to it. Here, after the control exits from the functions (fun2 or fun3), the memory cell has lost its name(which was "i " till the control was in fun2 or fun3), but the address of the cell remains the same, so i shud be able to access it correctly using pointers. It is happening as if the compiler resets the value of the memory cell (to zero if earlier it was float and garbage if it was int) as soon as the memory cell loses its name. And if this is happening (by any chance), why is the compiler doing this ??

I appreciate any help you can give......
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Getting wrong answer while returning address (but right if I return value)

 
0
  #2
Jun 6th, 2009
>If i use float instead of double, i get the answer 25.628401 (which is wrong...
1. It's the same question as Why Cessna 182 can't be so fast as F-22?: float data type has precision ~6-7 decimal digits only...

2. and 3. "dye" means dye: memory cells allocated (as usually in so called stack memory) for automatic (local) variables are reusable storage pool. In your case they were overwrited by the next printf call. That's why you got so strange results. Moral: never return pointers to local variables...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Getting wrong answer while returning address (but right if I return value)

 
0
  #3
Jun 6th, 2009
you cant pass the address of the local variable. They are destroyed after the function ends. Even the pointer which you declare is local. So, if you want to really use pointers pass all the pointers from the main function into the corresponding function.
Last edited by s_sridhar; Jun 6th, 2009 at 11:56 pm.
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Getting wrong answer while returning address (but right if I return value)

 
0
  #4
Jun 7th, 2009
>So, if you want to really use pointers pass all the pointers from the main function into the corresponding function.
It's too restrictive and unrealistic requirement.

>Even the pointer which you declare is local.
It's of no importance after return pointer;
However fun2 and fan3 OP functions are classic examples of never do that codes (like void main() instead of standard int main() .
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Getting wrong answer while returning address (but right if I return value)

 
0
  #5
Jun 7th, 2009
>It's too restrictive and unrealistic requirement

Yeah of course, I agree with you, but if he still wants to learn the working of pointers, he can do that.
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC