Function pointer in C, applications you should know

c function pointer

In C function pointer is like a normal pointer which is points some piece of the code that is used in runtime binding and resolves many problems. For this article, I am assuming that you have basic knowledge of function pointers. This article mainly focuses on the applications of function pointer where it plays an important role.

Call-back function:

In C language, we can implement the call-back using the function pointer. A call-back function is an important feature of any programing language, it keeps the reference to executable code that is passed as an argument to the other function and where it is used to call the referenced executable code.

Generally, the call-back function is used in library API because the library API is not only used by you but it is also used by the third person. If you use hard-coded function name in the library API then it will create issues.

The advantage of the call-back is that in library API caller does not need to know about the callee function, it only knows the prototypes of the callee function.

Let’s see the below program where I am describing how is the function pointer is work.

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

C tutorial

To access the methods from the DLL

In an application when we are used DLL then to access the DLL method (subroutine), we need to use a function pointer.

You can also See: how to create DLL

Let’s see an example, in this example code I am assuming “mathlibrary.dll” is a DLL which has many functions to perform the mathematical operation like as addition, subtraction…etc. If we require any one of the function, then we must create a function pointer which has the same prototype of the calling function.

 

To replace a nested switch using the array of a function pointer

One of the uses of function pointer is to replace the nested switch. Let’s understand it with below the example is shown below., In this code, I have a nested switch case, and I will remove the nested switch case using the array of the function pointer.

In this code, every state has three sub-states and each sub-states has a function which performs the mathematical operation. If the user selects the arithmetic state (state == Arithmetic), the user can select any of the three sub-states, each sub-state has a function which performs a mathematical operation. If user select first sub-state (substate1) than addition will be performed.

To display the result of the mathematical operation user needs to select the message state (state == Message) and after that corresponding sub-state.

Using the array of the function pointers, we can replace the above code with a 2D array.

 

Aticleworld invites you to try skillshare (Unlimited Access to over 20,000 classes) Premium free for 2 months.

Function pointer in MCU Boot-loader

An important application of function pointer is when we need to jump from the bootloader to the application in MCU. I have worked on a project, where we were required to upgrade the firmware of the device from the Wi-Fi. In this project, we have created a bootloader to upgrade the firmware of the device. In bootloader, we have verified the CRC of the firmware, if CRC is intact, the control jumps from the bootloader to the application.

Let’s see an example code to how to jump from bootloader to the application using the function pointer.


Polymorphism with the function pointer

Using the function pointer, we can create an illusion of polymorphism. This concept is useful where we need to a runtime polymorphism. To accomplish this task we need to create a function pointer within the structure and initialize the function pointer as per the requirements.

Let’s see an example, suppose there is a structure in c which contains function pointers, this function pointer stores the address of the function that calculates salary of an employee (Grad1 and Grad2 officer).

state machine implementation in c

A finite state machine is one of the popular design patterns, it has multiple states. Its state can be changed by an internal or external input. Input could be signal, hardware or software interrupt, timer expiry…etc. In the finite state machine, the procedure to change one state to another state is called transition.

We can implement a state machine in C using the array of function pointer, function pointer store the address of the callback which will be invoked at defined state and events.

For more detail see: finite state machine in c


Leave a Reply