2010年6月19日 星期六

function pinter in c (or c++)

/*
funpt.c: a demo of function pointer

to compile:
gcc -o funpt funpt.c -lm

Ching-Han Hsu, Ph.D. (c) 2010
*/

#include
#include
#include

double fun1(double (*fp)(double), double x)
{
printf("in fun1\n");
return fp(x);
}

double sqcos(double x)
{
printf("squared cos\n");
return (cos(x) * cos(x));
}

int main (int argc, char** argv)
{

printf("cos 2.0 = %.3f\n", cos(2.0));
printf("cos 2.0 = %.3f\n", fun1(cos, 2.0));
printf("squared cos 2.0 = %.3f\n", fun1(sqcos, 2.0));

return 0;
}

沒有留言: