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.
What 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);
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, orvoid. - 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 which refers to a function that has two integer parameters and return an integer.
int (*fpFunc)(int x,int y); // declare a function pointer
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 as arguments. 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;
}Next, we declare a function pointer that will refer to the compare() function:
int (*fpComparer)(int x,int y);
Then, inside the main() function, we can assign address of the compare() function to the function pointer:
fpComparer = &compare;
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);
Putting it all together.
/*
* File: main.c
* Author: zentut.com
* 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;
}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.
/*
* File: main.c
* Author: zentut.com
* 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;
}Function that returns a function pointer
A function can return a function pointer. Let’s take a look at the following example:
/*
* File: main.c
* Author: zentut.com
* 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 '-':
return −
break;
default:
return NULL;
}
}
int plus(int x,int y)
{
return x + y;
}
int minus(int x,int y)
{
return x - y;
}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 select a function to execute at runtime.
The following example demonstrates how to use an array of function pointers.
/*
* File: main.c
* Author: zentut.com
* 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 '-':
return −
break;
default:
return NULL;
}
}
int plus(int x,int y)
{
return x + y;
}
int minus(int x,int y)
{
return x - y;
}In this tutorial, we have discussed about C function pointer, what is is and how to use a function pointer in your code.