next up previous contents index
Next: Type qualifiers (advanced) Up: Types Previous: The void type   Contents   Index


Function pointers (advanced)

Function pointers are a complex and very powerful feature of C. They allow you to pass one function as an argument to another function. We will not describe function pointers here, except for an illustration of how a function can be passed to a CCATSL function, and how to work-out from the declaration of the CCATSL function how your function must be declared:

  
double f(double x) {
  return x*x-1.0;
}

int main(void) {
 
  double ans, err;

  /*  integrate f over 0, 1 using 256 sub-intervals  */
  ans = RombergCL(f, 0.0, 1.0, &err, 8);
}
The CCATSL function RombergCL is declared as:

  
double RombergCL(double a, double b, int n, double (*f)(double x),
       double *err);
The fourth argument is a pointer to a function which accepts one double argument and returns a double. (Whenever you use the name of a function as an argument to another function, it is treated as a pointer.) Similarly an argument declared as

  
void (*fp)(int, double, const char*)
is a pointer to a function which takes an int, a double and a char*, it returns nothing (it returns a void), and promises not to modify the memory pointed to by the final argument (the const qualifier). Pointers are described in detail here.


next up previous contents index
Next: Type qualifiers (advanced) Up: Types Previous: The void type   Contents   Index
CATAM admin 2010-02-23