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:

<return_type> (*<pointer_name>) (function_arguments);
Code language: C++ (cpp)

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.

int (*fpFunc)(int x,int y); // declare a function pointer
Code language: C++ (cpp)

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:

int compare(int,int); /* Purpose: compare x and y Return 1 if x > y, -1 if x < y and 0 if x = y */ int compare(int x,int y) { if(x > y) return 1; if(x < y ) return -1; return 0; }
Code language: C++ (cpp)

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

int (*fpComparer)(int x,int y);
Code language: C++ (cpp)

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

fpComparer = &compare;
Code language: C++ (cpp)

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:

result = (*fpComparer)(a,b);
Code language: C++ (cpp)

Putting it all together.

/* * File: main.c * Author: learnc.net * C Function Pointer Demo 1 */ #include <stdio.h> #include <stdlib.h> /* declare a function pointer */ int (*fpComparer)(int x,int y); int compare(int,int); int main() { int result; int a = 10; int b = 20; char* msg; fpComparer = &compare; result = (*fpComparer)(a,b); switch(result) { case 1: printf("a is greater than b"); break; case -1: printf("a is less than b"); break; case 0: printf("a is equal to b"); break; } return 0; } /* Purpose: compare x and y return 1 if x > y, -1 if x < y and 0 if x = y */ int compare(int x,int y) { if(x > y) return 1; if(x < y ) return -1; return 0; }
Code language: C++ (cpp)

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  the qsort() function to sort an array of integers.

/* * File: main.c * Author: learnc.net * C function Pointer Demo 2 */ #include <stdio.h> #include <stdlib.h> #include <string.h> int compare_int(const int * x,const int * y); int (*fpComparer)(const void *p, const void *q); int main() { int i; // sort integer array int int_arr[] = { 7, 3, 4, 1, -1, 23, 12, 43, 2, -4, 5 }; size_t len = sizeof(int_arr)/sizeof(int); fpComparer = &compare_int; qsort(int_arr, len, sizeof(int), (*fpComparer)); // print array after sorting for(i = 0; i < len; i++) { printf("%d ",int_arr[i]); } printf("\n"); } int compare_int(const int * x,const int * y) { if (*x == *y) return 0; else if (*x < *y) return -1; else return 1; }
Code language: C++ (cpp)

Function that returns a function pointer

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

/* * File: main.c * Author: learnc.net * C function Pointer Demo 3 */ #include <stdio.h> #include <stdlib.h> /* define function poiter type */ typedef int(*pfOperator)(int, int); int plus(int,int); int minus(int,int); pfOperator getOperator(const char oper); int main() { int x = 10, y = 20, z = 0; pfOperator func = NULL; func = getOperator('+'); z = func(x,y); printf("%d\n",z); func = getOperator('-'); z = func(x,y); printf("%d\n",z); return 0; } pfOperator getOperator(const char oper) { switch(oper) { case '+': return + break; case '-': returnbreak; default: return NULL; } } int plus(int x,int y) { return x + y; } int minus(int x,int y) { return x - y; }
Code language: C++ (cpp)

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.

/* * File: main.c * Author: learnc.net * C Function Pointer Demo 4 */ #include <stdio.h> #include <stdlib.h> /* define function pointer type */ typedef int(*pfOperator)(int, int); int plus(int,int); int minus(int,int); pfOperator getOperator(const char oper); int main() { int x = 10, y = 20, z = 0; // declare function pointer array pfOperator funcs[2]; funcs[0] = getOperator('+'); z = (funcs[0])(x,y); printf("%d\n",z); funcs[1] = getOperator('-'); z = (*funcs[1])(x,y); printf("%d\n",z); return 0; } pfOperator getOperator(const char oper) { switch(oper) { case '+': return + break; case '-': returnbreak; default: return NULL; } } int plus(int x,int y) { return x + y; } int minus(int x,int y) { return x - y; }
Code language: C++ (cpp)

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

Was this tutorial helpful ?