Each function has its own address and a pointer variable can also be used to store the address of the function.
Thus the function pointer is a pointer that holds the address of a function. It is used to achieve a callback mechanism where the function is called at a certain event, a kind of event-driven mechanism at run time.
Callback Mechanism:
If the reference of a function is passed as an argument to another function to call it, it is known as the callback function. This is achieved using a function pointer when the address of the function is passed to another function to call it.
Please refer to the topics below for a clear understanding before reading further:
- Array in C
- Introduction to pointer
- Pointer declaration, initialization, and dereferencing
- Pointer to array
Syntax:
data_type (*fun_name) (arguments1, arguments2..)
Steps to use the Function pointer:
- Declare a function pointer:
data_type (*ptr) (param1, param2, ..);
- Assign a function address to the pointer:
ptr = fun;
- Call the function:
(ptr) (param1, param2, ..);
Example:
#include <stdio.h>
int add (int num1, int num2)
{
return num1 + num2;
}
int main ()
{
//Declare a function pointer
int (*ptr) (int, int);
//Assign function address to pointer
ptr = fun;
//Call function pointer
int ret = (ptr) (3,4);
printf("\n The sum is: %d", ret);
return 0;
}
Output:
The sum is: 7
Array of function pointer:
Array of a function pointer is an array which holds the address of function at each index.
Syntax:
- Declare the function pointer:
int (*ptr[Size]) (int, int)
- Assign the address to the pointer:
ptr[0] = fun1;
ptr[1] = fun2;
- Call the function:
(ptr[0]) (3,4)
(ptr[1]) (6,5)
Example:
#include <stdio.h>
int add (int num1, int num2)
{
return num1 + num2;
}
int sum (int num1, int num2)
{
return num1 - num2;
}
int main ()
{
//Declare a function pointer
int (*ptr[2]) (int, int);
//Assign function address to pointer
ptr[0] = add;
//Call function pointer
int ret = (ptr[0]) (3,4);
printf("\n The sum is: %d", ret);
ptr[1] = sum;
ret = (ptr[1]) (6,4);
printf("\n The difference is: %d", ret);
return 0;
}
Output:
The sum is: 7
The difference is: 2
Return a function pointers:
#include <stdio.h> int sum (int a, int b) { return (a + b); } int (*fun())(int, int) { return sum; } int main() { int (*ptr) (int, int); ptr = fun(); printf("\n The sum is: %d",(ptr)(3,4)); return 0; }
Output:
The sum is: 7
Passing function pointer: (callback function)
#include <stdio.h> void sum (int a, int b) { printf("\n The sum is: %d\n", a + b); } void fun(void (*sum)()) { sum(3,4); } int main() { fun(sum); return 0; }
The sum is: 7
Use-case of function pointer:
One good use case of function pointers is when different functions are needed at different times or different phases of development.
For example:
During the development phase, the output should be redirected to the console and at the release time, it should be redirected to the log file.
This can be handled with the help of a function pointer and with macros.
version.h
// Mimics the printf function prototype. This will be used to actually
// use to print stuff to the screen
void (* zprintf)(const char*, ...);
void noprintf(const char* c, ...)
{
return;
}
version.c
void init ()
{
#define DEBUG_VERSION
#ifdef DEBUG_VERSION
zprintf = &printf;
#else
zprintf = &nonprintf;
#endif
}
Once it is referenced in the application, it will start executing wherever it is pointed.
MainProgram.c
#include "version.h"
#include <stdlib.h>
int main()
{
// Must run init(), which assigns the function
// pointer to an actual function
init();
void *ptr = malloc(100); // Allocate 100 bytes of memory
// malloc returns NULL if unable to allocate the memory.
if (ptr == NULL)
{
zprintf("Unable to allocate memory\n");
return 1;
}
// Other things to do...
return 0;
}
The above code will be used printf if in debug mode or do nothing if in release mode. This is much easier than going through the entire project and commenting out or deleting code. All we need to do is change the version in version.h
and the code will do the rest.
Categories: C Language
Leave a Reply