C Function Pointer

Summary: in this tutorial, you will learn about C function pointer, which is a special pointer that points to a function instead of a data object.

C function pointerWhat is a C function pointer

A function pointer is a pointer that refers to the address of a function.

C function pointer syntax

The following illustrates the syntax of declaring a function pointer:

The syntax of declaring a function pointer is similar to the syntax of declaring a function. The only difference is that instead of using the function name, you use a pointer name inside the parentheses ().

Let’s examine the function pointer syntax above in more detail:

  • First, you specify the return type of the function pointer. It can be any valid type such as int, float, char, or void.
  • Second, you put the name of function pointer inside parentheses. By convention, the name of function pointer begins with fp.
  • Third, you specify all parameters of the function with their corresponding types. Notice that the function pointer only can refer to a function with the same signature. It means all functions, which the function pointer refers to, must have the same return type and parameters.

The following example declares a function pointer referred to a function that accepts two integer parameters and returns an integer.

Using function pointers

Before using a function pointer, you need to assign it an address of a function.

First, suppose you have a compare() function that accepts two integers a and b. The compare() function returns 1 if a > b, 0 if a = b and -1 if a < b.

The following is the compare() function’s header and implementation:

Next, we declare a function pointer that refers to the compare() function:

Then, inside the main() function, we can assign address of the compare() function to the function pointer:

Notice that the unary operator & is optional. However to make your code more portable, you should always use unary operator ( &) when you assign an address of a function to a function pointer.

Finally, we can call the function using the function pointer as follows:

Putting it all together.

Passing function pointer an as argument to a function

You can pass a function pointer as an argument to a function. This allows you to make your code more flexible. A classic example of using a function pointer as an argument is the qsort() function in the function library. The qsort() function sorts an array of any type using the quicksort algorithm.

The following example demonstrates how to use qsort() function to sort an array of integers.

Function that returns a function pointer

A function may return a function pointer. Let’s take a look at the following example:

Array of function pointers

An array of function pointers includes all function pointers that have the same signature. An array of function pointers allows you to choose which function to execute at runtime.

The following example demonstrates how to use an array of function pointers.

In this tutorial, we have discussed about C function pointer and shown you how to use the  function pointer to make the code more flexible.