[comp.lang.c] passing address of floating-point parameter

cjross@bbn.com (chris ross) (06/12/91)

Here's something to feed to your compiler.

Define a routine which takes a (float) parameter and passes the address to a
routine which expects a (float*).  What value do you get when following the
pointer in the second routine?  Under Vax Ultrix and Masscomp RTU, I get
what I passed the first routine.  Under SunOS and MIPS RISC/OS, I get
something different.

Here's the code.  It should print
  1 1
  1 1


main ()
{
    foo(1.0, 1.0);
}

foo (x, y)
float x, y;
{
    float local_x = x;
    show(&local_x, &y);
    printf("%g %g\n", x, y);
}

show (xp, yp)
float *xp, *yp;
{
    printf("%g %g\n", *xp, *yp);
}


Apparently, the Sun and MIPS compilers do not hide the fact that the
parameter in the first routine is actually on the stack as a (double).
Should they, or, as with va_arg, must the programmer explicitly take the
type promotion into account?  Does ANSI specify the proper behavior?


----------  chris ross  ----------  <cjross@bbn.com>  ----------
lisp in action is like a finely choreographed ballet.
ada in action is like a waltz of drugged elephants.
c in action is like a sword dance on a freshly waxed floor.

worley@compass.com (Dale Worley) (06/14/91)

In article <64623@bbn.BBN.COM> cjross@bbn.com (chris ross) writes:

   foo (x, y)
   float x, y;
   { }

   Apparently, the Sun and MIPS compilers do not hide the fact that the
   parameter in the first routine is actually on the stack as a (double).
   Should they, or, as with va_arg, must the programmer explicitly take the
   type promotion into account?  Does ANSI specify the proper behavior?

According to ANSI C, x and y are passed in as doubles but must be
converted to floats upon entry.  In particular, &x has type "pointer
to float".  However, most K+R compilers promote x and y to doubles, so
&x has type "pointer to double".  That is, not only is the value
passed in as a double, it remains a double once it is passed in.
Section 3.7.1 of the Rationale describes this situation, and notes it
as a "Quiet Change".

Dale Worley		Compass, Inc.			worley@compass.com
--
If there is a bedrock principle underlying the First Amendment,
it is that the Government may not prohibit the expression of an
idea simply because society finds the idea itself offensive or
disagreeable.
-- Justice William J. Brennan, expressing the opinion of the
   Supreme Court in "Texas v. Johnson" (1989, flag-burning)