Nasty bug

Walter Murray walter at hpcllca.HP.COM
Fri Aug 31 02:14:33 AEST 1990


Henrik Sandell writes:

>  I had a lot of trouble with a bug yesterday. The code was similar to the
> following:

[Program using an old-style (non-prototype) function definition with
a parameter of type float]

>And it didn't work. Why? The answer is that the parameter f is a
>double, not a float since all floats are converted to double when they are
>passed as arguments to functions. Therefore &f is a ptr to double rather than a
>ptr to float as one would expect looking at the declartion.

> Well, I hope I'll help someone to avoid that problem... It took me a while to
>figure out why the code didn't work...

You got bit by what is called type rewriting, something that was done by
some pre-ANSI compilers.  Although you declared the parameter as float,
the compiler acted exactly as if you had declared it as double.  Some
compilers may also do this with parameters of type char or short, pretending
that you had actually declared them as int.

Type rewriting is not permitted for a compiler that conforms to the
ANSI standard.  A program that relies on type rewriting may not behave
as expected when compiled with a standard-conforming compiler.  For
more information, see section 3.7.1 of the ANSI C Rationale.

Walter Murray
------



More information about the Comp.lang.c mailing list