Pointers in C: Pointers to Functions

Of a slightly more advanced nature, but presented here for the sake of completeness, is the notion of a pointer to a function. When working with pointers to functions, the C compiler needs to know not only that the pointer variable points to a function, but also the type of value returned by that function as well as the number and types of its argu- ments. To declare a variable fnPtr to be of type “pointer to function that returns an int and that takes no arguments,” the declaration

int (*fnPtr) (void);

can be written. The parentheses around *fnPtr are required because otherwise the C compiler treats the preceding statement as the declaration of a function called fnPtr that returns a pointer to an int (because the function call operator () has higher precedence than the pointer indirection operator *).

To set your function pointer pointing to a specific function, you simply assign the name of the function to it. So, if lookup is a function that returns an int and that takes no arguments, the statement

fnPtr = lookup;

stores a pointer to this function inside the function pointer variable fnPtr.Writing a function name without a subsequent set of parentheses is treated in an analogous way to writing an array name without a subscript. The C compiler automatically produces a pointer to the specified function. An ampersand is permitted in front of the function name, but it’s not required.

If the lookup function has not been previously defined in the program, it is necessary to declare the function before the preceding assignment can be made. So, a statement such as

int lookup (void);

is needed before a pointer to this function can be assigned to the variable fnPtr.

You can call the function that is indirectly referenced through a pointer variable by applying the function call operator to the pointer, listing any arguments to the function inside the parentheses. For example,

entry = fnPtr ();

calls the function pointed to by fnPtr, storing the returned value inside the variable entry.

One common application for pointers to functions is in passing them as arguments to other functions. The standard C library uses this, for example, in the function qsort, which performs a “quicksort” on an array of data elements. This function takes as one of its arguments a pointer to a function that is called whenever qsort needs to compare two elements in the array being sorted. In this manner, qsort can be used to sort arrays of any type, as the actual comparison of any two elements in the array is made by a user- supplied function, and not by the qsort function itself. Appendix B, “The Standard C Library,” goes into more detail about qsort and contains an actual example of its use.

Another common application for function pointers is to create what is known as dis-patch tables.You can’t store functions themselves inside the elements of an array. However, it is valid to store function pointers inside an array. Given this, you can create tables that contain pointers to functions to be called. For example, you might create a table for processing different commands that will be entered by a user. Each entry in the table could contain both the command name and a pointer to a function to call to process that particular command. Now, whenever the user enters a command, you can look up the command inside the table and invoke the corresponding function to handle it.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

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