Nude Vicar in Sex Romp!

Walter Murray walter at hpclwjm.HP.COM
Fri Aug 18 10:09:39 AEST 1989


Andy Yule writes:

> #include <stdio.h>
> main()
>  {
>     float x= 0.1234;
>     fn1(x);
>  }
> fn1(x)
> float x;
>  {
>     float y= x;
>     fn2(&x, &y);
>  }
> fn2(x, y)
> float *x, *y;
>  {
>     printf("In fn2 values are x= %f & y= %f\n", *x, *y);
>  }

> [Conjecture as to why the first value printed is gibberish]

> What I would like to know is whether this is what the compiler
> should do (I've looked in K&R and I couldn't find anything
> that addressed this problem specifically).

A number of C implementations do this.  In fn1(), the compiler is
rewriting the type of x to double, knowing that that's how it is
going to be passed.  All behavior will be exactly as though you
had written 'double x' rather than 'float x'.  Try printing
sizeof(x) and you will see that it is equal to sizeof(double),
not sizeof(float).

Your understanding of what is happening is correct.  Some compilers
do this type rewriting for integral parameter types as well as for
floating types, although HP only does it for floating types.

You will be glad to know that the dpANS outlaws this behavior.
Run your program on an ANSI-conforming compiler and it will
print 0.123400 for both *x and *y.  If you have a copy of the
dpANS, see section 3.7.1 of the Rationale.

Walter Murray
----------



More information about the Comp.lang.c mailing list