EmbLogic's Blog

What is Function Pointer

A function pointer (or subroutine pointer or procedure pointer) is a type of pointer supported by third-generation programming languages (such as PL/I, COBOL, Fortran,[1] dBASE dBL, and C) and object-oriented programming languages (such as C++ and D).[2]

Instead of referring to data values, a function pointer points to executable code within memory. When dereferenced, a function pointer can be used to invoke the function it points to and pass its arguments just like a normal function call. Such an invocation is also known as an “indirect” call, because the function is being invoked indirectly through a variable instead of directly through a fixed name or address.

Function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

The simplest implementation of a function (or subroutine) pointer is as a variable containing the address of the function within executable memory. Older third-generation languages such as PL/I and COBOL, as well as more modern languages such as Pascal and C generally implement function pointers in this manner.[3]

The following C program illustrates the use of two function pointers:

func1 takes one double-precision (double) parameter and returns another double, and is assigned to a function which converts centimetres to inches
func2 takes a pointer to a constant character array as well as an integer and returns a pointer to a character, and is assigned to a string library function which returns a pointer to the first occurrence of a given character in a character array

#include /* for printf */
#include /* for strchr */

double cm_to_inches(double cm) {
return cm / 2.54;
}

int main(void) {
double (*func1)(double) = cm_to_inches;
char * (*func2)(const char *, int) = strchr;
printf(“%f %s”, func1(15.0), func2(“Wikipedia”, ‘p’));
/* prints “5.905512 pedia” */
return 0;
}

The next program uses a function pointer to invoke one of two functions (sin or cos) indirectly from another function (compute_sum, computing an approximation of the function’s Riemann integration). The program operates by having function main call function compute_sum twice, passing it a pointer to the library function sin the first time, and a pointer to function cos the second time. Function compute_sum in turn invokes one of the two functions indirectly by dereferencing its function pointer argument funcp multiple times, adding together the values that the invoked function returns and returning the resulting sum. The two sums are written to the standard output by main.

#include
#include

// Function taking a function pointer as an argument
double compute_sum(double (*funcp)(double), double lo, double hi) {
double sum = 0.0;

// Add values returned by the pointed-to function ‘*funcp’
int i;
for(i = 0; i <= 100; i++) {
double x, y;

// Use the function pointer 'funcp' to invoke the function
x = i / 100.0 * (hi – lo) + lo;
y = (*funcp)(x);
sum += y;
}
return sum / 101.0;
}

int main(void) {
double (*fp)(double); // Function pointer
double sum;

// Use 'sin()' as the pointed-to function
fp = sin;
sum = compute_sum(fp, 0.0, 1.0);
printf("sum(sin): %f\n", sum);

// Use 'cos()' as the pointed-to function
fp = cos;
sum = compute_sum(fp, 0.0, 1.0);
printf("sum(cos): %f\n", sum);
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>