Learn all about function pointer in C programming language with coding, explanation and examples of functions and pointers.
Almost every function includes variables, constants and other identifiers. It is known that all of these identifiers are allocated some memory space. Therefore, every function also gets some memory allocation.
What Is A Function Pointer?
The function pointers are pointer variables that point to the address of a function. Similar to other pointer variables, the function pointers can be declared, assigned values and used to access the functions which they point to.
A pointer to a function points to the initial portion of the executable code of the function. It points to code and not the data.
Syntax
1 | return_type (*function_pointer_name)(argument_list); |
Declaring a Function Pointer
In order to declare a pointer to a function, we have to declare it like the prototype of the function except that the name of the function is enclosed in parentheses and an asterisk is inserted before the name.
1 | int (*user_defined_function)(int a, float b); |
In function pointer declaration above, we declare a pointer to a function that returns an integer value and accepts two arguments – one of type integer and the other of type float.
Because of precedence of operators, if you do not put the function name within parentheses, you will end up declaring a function returning a pointer as shown below:
1 | int *user_defined_function(int a, float b); |
When a pointer to a function is declared, it can be called using one of the following forms:
1 | (*user_defined_function)(1, 2); |
1 | user_defined_function(1, 2); |
Note: This C function pointer coding example is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.
C Program For Arithmetic Operations using Function Pointer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include<stdio.h> float (*func)(float, float); float add(float, float); float subtract(float, float); float multiply(float, float); float divide(float, float); int main() { float x, y; printf("\nEnter value of X:\t"); scanf("%f", &x); printf("\nEnter value of Y:\t"); scanf("%f", &y); func = add; printf("\nAddition:\t%f", func(x, y)); func = subtract; printf("\nSubtraction:\t%f", func(x, y)); func = multiply; printf("\nSubtraction:\t%f", func(x, y)); func = divide; printf("\nSubtraction:\t%f", func(x, y)); return 0; } float add(float x, float y) { return (x + y); } float subtract(float x, float y) { return (x - y); } float multiply(float x, float y) { return (x * y); } float divide(float x, float y) { return (x / y); } |
Output

How To Pass A Function Pointer As An Argument To A Function?
A function pointer can be passed as the calling argument of a function and can also be returned from a function. This is, in fact, necessary if you want to pass a pointer to a callback function. Let us see an implementation below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> int add(int x, int y); int subtract(int x, int y); int divide(int x, int y); int multiply(int x, int y); int operate(int (*operate_fp)(int, int), int x, int y); int main() { int x, y; printf("\nEnter value of X:\t"); scanf("%d", &x); printf("\nEnter value of Y:\t"); scanf("%d", &y); printf("\nAddition:\t%d\n", operate(add, x, y)); printf("\nSubtraction:\t%d\n", operate(subtract, x, y)); printf("\nMultiplication:\t%d\n", operate(multiply, x, y)); printf("\nDivision:\t%d\n", operate(divide, x, y)); } int operate(int (*operate_fp)(int, int), int x, int y) { return (*operate_fp)(x, y); } int add(int x, int y) { return (x + y); } int subtract(int x, int y) { return (x - y); } int multiply(int x, int y) { return (x * y); } int divide(int x, int y) { return (x / y); } |
Array of Function Pointers
When an array of function pointers is made, the appropriate function is selected using an index.
Step 1: Use typedef keyword so that function_pointer can be used as type:
1 | typedef int (*function_pointer)(int, int); |
Step 2: Define the array and initialize each element to NULL. This can be done in the following ways:
1 | function_pointer function_array[10] = {NULL}; |
1 | int (*function_array[10])(int, int) = {NULL}; |
Step 3: Assign the function’s address – Add, Subtract, Multiply and Divide
1 2 3 4 | function_array[0] = &add; function_array[1] = &subtract; function_array[2] = &multiply; function_array[3] = ÷ |
Step 4: Call the function using an index to address the function pointer
1 | printf("%d\n", *function_array[0] (5, 10)); |
Let us see a complete implementation of array of function pointers in C programming below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> int add(int x, int y); int subtract(int x, int y); int divide(int x, int y); int multiply(int x, int y); int (*function_pointer[4]) (int x, int y); int main() { int result; int x, y, operation; function_pointer[0] = add; function_pointer[1] = subtract; function_pointer[2] = multiply; function_pointer[3] = divide; printf("\nEnter value for X:\t"); scanf("%d", &x); printf("\nEnter value for Y:\t"); scanf("%d", &y); do { printf("\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\n"); printf("What's your choice?:\t"); scanf("%d", &operation); result = (*function_pointer[operation]) (x, y); printf("\nResult:\t%d", result); }while(operation != 4); return 0; } int add(int x, int y) { return (x + y); } int subtract(int x, int y) { return (x - y); } int multiply(int x, int y) { return (x * y); } int divide(int x, int y) { return (x / y); } |
If you have any doubts about function pointer in C programming, let us know about it in the comment section. Find more about it here.
MORE ON POINTERS |
---|
Null Pointers |
Void Pointers |
Wild Pointers |
Dangling Pointers |
Huge Pointers |
Near Pointers |
Far Pointers |