Problem returning doubles from a function
Tim_CDC_Roberts at cup.portal.com
Tim_CDC_Roberts at cup.portal.com
Sat Mar 25 10:46:13 AEST 1989
In <39722 at csvax1.cs.tcd.ie>, omahony at csvax1.cs.tcd.ie (Donal O'Mahony) writes:
> Can anone explain the problem with the following program (made up of
> 2 files).
>
> ::main.c
> #include <stdio.h>
> main()
> {double a,b;
>
> a = 1.234;
> b = store_length(a);
> printf("b=%f\n",b);
> }
>
> ::rt1.c
> double store_length( double measurements)
> { measurements= 2;
> printf("Returning measurements=%f\n",measurements);
> return(measurements);
> }
>
> When these two programs are linked and run, they do not print 'b=2,'
> as I would expect. Why?
Once again, I am sure there will be 24 other replies to this, all of which
will cross in the net.
In main.c, since you did not declare that store_length returns a double,
the compiler will assume it returns an int. It will then try to cast that
to a double, thereby rendering it undecipherable.
Modify main.c thusly:
#include <stdio.h>
double store_length (double);
main()
{double a,b;
a = 1.234;
b = store_length(a);
printf("b=%f\n",b);
}
(Note to those who will warn me about the ANSI header: it can be
deduced from his file rt1.c that he is using a compiler which
supports the prototypes.)
More information about the Comp.lang.c
mailing list