Re: double pointers



vaavi said:

Hi,what is the advantage of using the double pointers?

They're useful when you want a function to update the value of a double,
and have that change "stick" when the function returns. Because C is
pass-by-value, passing a double doesn't work, so you pass a double pointer
instead, and update the value of the object (the double) to which that
pointer points. Here's an example, where we want to calculate sin, cos and
tan of an angle, all in a single call:

#include <stdio.h>
#include <math.h>

/* don't call this if tan(angle) doesn't make sense! */
double sincostan(double angle, double *pcosine, double *ptangent)
{
double sine = sin(angle);
double cosine = cos(angle);
double tangent = tan(angle);
if(pcosine != NULL)
{
*pcosine = cosine;
}
if(ptangent != NULL)
{
*ptangent = tangent;
}
return sine;
}

int main(void)
{
double angle = 2.17828;
double cosine = 0.0;
double tangent = 0.0;
double sine = sincostan(angle, &cosine, &tangent);
printf("sin %f = %f\n", angle, sine);
printf("cos %f = %f\n", angle, cosine);
printf("tan %f = %f\n", angle, tangent);
return 0;
}

Output on my system:

sin 2.178280 = 0.821087
cos 2.178280 = -0.570803
tan 2.178280 = -1.438477

I got many c programs using the double pointers.

Strange. They are sometimes useful, but I wouldn't have thought they were
*that* common.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: Maxwells Equations
    ... > which simulated altitude with a voltage. ... The first derivative of the sine function is the cosine function. ... Tangent is just a ratio, ...
    (sci.physics.relativity)
  • Re: Maxwells Equations
    ... > which simulated altitude with a voltage. ... The first derivative of the sine function is the cosine function. ... Tangent is just a ratio, ...
    (sci.physics.relativity)
  • Re: how to get the angle from the cosine, etc.
    ... >that you already have the sine and cosine of the angle. ... What we are forced to do is to _define_ the inverse cosine as a new ... If you also know the sine, then you have a unique solution within ...
    (sci.math)
  • Re: Cam Complaint
    ... I'm a moldmaker and all we use is tangent. ... If you have a triangle, you tangent the angle, if you know the line needs ... Works for everything there is....except the dekel cutter grinder. ... making a cutter it's technically a sine, but we use tangent anyway, and up ...
    (alt.machines.cnc)
  • Re: Golly, that was easy...
    ... addition formulas for cosine and sine. ... 'a' to keep track of both the sine and cosine of the angle. ...
    (sci.physics)