Re: double pointers
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Thu, 19 Jun 2008 11:24:31 +0000
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
.
- Follow-Ups:
- Re: double pointers
- From: Bartc
- Re: double pointers
- From: thomas . mertes
- Re: double pointers
- References:
- double pointers
- From: vaavi
- double pointers
- Prev by Date: double pointers
- Next by Date: Re: double pointers
- Previous by thread: double pointers
- Next by thread: Re: double pointers
- Index(es):
Relevant Pages
|