| | |
Getting wrong answer while returning address (but right if I return value)
![]() |
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
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 '
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> float fun1(float,float); float *fun2(float,float); int *fun3(int,int); void main() { float i,j,k,prod,*l; int o=3,m=2,*n; i=7.56; j=3.39; k=fun1(i,j); l=fun2(i,j); n=fun3(o,m); prod=i*j; printf("the product =\n\n%f",prod); printf("\n\nthe product(from function -call by value-return value) = %f",k); printf("\n\nThe product (from function -call by value-return pointer)= %f",*l); printf("\n\nThe product of integers -return pointer= %d",*n); } float fun1(float i,float j) { float prod; prod=i*j; return prod; } float *fun2(float i, float j) { float *p,prod; prod=i*j; p=∏ return p; } int *fun3(int i,int j) { int prod,*p; prod=i*j; p=∏ return p; }
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......
>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...
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...
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
>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
However fun2 and fan3 OP functions are classic examples of never do that codes (like
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() .
![]() |
Similar Threads
- Karatsuba Wrong Answer After 6 Decimal digits (C++)
- Could you tell me what is wrong of my answer? (Pascal and Delphi)
- How to read complete file names??? (C)
- Can't figure out where my ACM sollution gives wrong answer. (C++)
- function returns address out of bounds (C)
- Placement new (C++)
- pointer and smart pointer address (C++)
- Why constructors don't have return types (C++)
- stripping digits (C)
Other Threads in the C Forum
- Previous Thread: pseudocode needed
- Next Thread: Execution priority question
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi






