Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!cs.utexas.edu!usc!apple!motcsd!hpda!hpcuhb!hpcllla!hpclisp!hpclwjm!walter From: walter@hpclwjm.HP.COM (Walter Murray) Newsgroups: comp.lang.c Subject: Re: Nude Vicar in Sex Romp! Message-ID: <660055@hpclwjm.HP.COM> Date: 18 Aug 89 00:09:39 GMT References: <960@prlhp1.prl.philips.co.uk> Organization: Hewlett-Packard Calif. Language Lab Lines: 44 Andy Yule writes: > #include> 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 ----------