[comp.sys.mac.programmer] Stupid LightspeedC question

CXT105@PSUVM.BITNET (Christopher Tate) (02/18/89)

Ok, here's another one.  I've written a couple of functions to convert from
degrees to radians and back, and I can't get them to work correctly (read: the
way I want them to).

When I use the following function declaration:

          #include <math.h>           /* this gets the PI definition */
          double deg_to_rad(x)
          double x;
          {
               return (x*PI/180);
          }

the routine returns the same value regardless of what I pass as x.  The only
way I have found to get it to work (based on the assumption that a parameter of
type double cannot be passed ordinarily, since it's 10 bytes long) is to
declare the function thus:

          #include <math.h>
          double deg_to_rad(x)
          double *x;
          {
               return ((*x)*PI/180);
          }

and pass a pointer to a predefined variable of type double as x.

The question is this:  is there any way I can rig it so I can pass the argu-
ment by value (as in the first example), so I can use constants as parameters?

-------
Christopher Tate      ||      "I hate quotations!" -- Ralph Waldo Emerson

CXT105@PSUVM.BITNET (Christopher Tate) (02/18/89)

Sorry, but one minor glitch.  The only working code I've come up with so far
looks like this:

          #include <math.h>

          void deg_to_rad(angle, x)
          double *angle;
          double *x;
          {
               *x = (*angle)*PI/180;
          }

I pass it pointers both to a dummy variable holding the desired angle value (in
degrees) and to the variable to which I'd like to assign the corrosponding
value in radians.

-------
Christopher Tate                   | somewhere i have never travelled,
cxt105@psuvm.psu.edu               | gladly beyond any experience,
...!psuvax1!psuvm.bitnet!cxt105    | your eyes have their silence.

tim@hoptoad.uucp (Tim Maroney) (02/19/89)

In article <72138CXT105@PSUVM> CXT105@PSUVM.BITNET (Christopher Tate) writes:
>          double deg_to_rad(x)
>          double x;
>          {
>               return (x*PI/180);
>          }
>
>the routine returns the same value regardless of what I pass as x.  The only
>way I have found to get it to work (based on the assumption that a parameter of
>type double cannot be passed ordinarily, since it's 10 bytes long) is to
>declare the function thus:
>
>          double deg_to_rad(x)
>          double *x;
>          {
>               return ((*x)*PI/180);
>          }

Arguments of type float are automatically converted to type double when
you pass them to C functions.  Arguments of type double are passed as
double.  If they aren't, your compiler is in error.  See Kernighan and
Ritchie, page 42.  I may be missing something, but your functions look
like they should have the same effect if both are called correctly.

One thing that occurs to me is that, if deg_to_rad is in a different
file from where it is called, you may have forgotten to declare it as
type double before you call it.  If so, then the compiler assumes it's
type int, and that would seem to explain the weird behavior you're
seeing.

This problem would also explain your second message, which said that
the above pass-by-reference form still doesn't work, and you have to
pass a pointer to where the return value should go.  Here's what you
said did work:

>          void deg_to_rad(angle, x)
>          double *angle;
>          double *x;
>          {
>               *x = (*angle)*PI/180;
>          }

Since there's no return value here, this supports the guess that
the problem is in a declaration (or lack thereof) of the return value.
-- 
Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim
"This signature is not to be quoted." -- Erland Sommarskog