How to use the structure of function pointer in c language

function pointer in c

A pointer is the backbone of c language, without the pointers, we cannot imagine the C Language. Good knowledge of pointers enables the programmer to write an optimize and robust code.

In the C language, a pointer is like a variable but the difference is that variable store the value and pointer store the address (Adress of a variable, array, function or pointer ..etc). A pointer can primitive type like int, char, and float. Or non-primitive type like structure, union, etc.

For Example,


In this article, I am discussing the use of function pointer in c within a structure and assuming that you have good knowledge of pointers and you are aware of the function pointers.

How to declare a function pointer in C within a structure

We know, C is not an object-oriented language, so it does not contain the member functions like C++. In the c language, a structure does not contain the functions, only contains the data. We can store a function within the structure using the function pointer in C and these function pointers treat like as a member function in C++.

function pointer in c
For example,

In below example, a structure contains some function pointers.


We can also  simplify the above declaration, by using the typedef with a function pointer



How to use function pointers in the structure

In polymorphism

We already know, C is not an object-oriented language and it does not provide the concept of ops. So when in a program, you intend to do some form of polymorphism, then function pointer come here like a friend.

For example,

In our case, we are communicating with the server in two ways, with SSL, and without SSL and most important thing is that it is not an end. Maybe in the future, we need to select the other way for server communication.

So here we think to create a structure with some function pointers. This structure represents the generic containers and the function pointers behave like C++ member function.


The above-declared structure behaves like a generic container for the server communication API.

We have created the above typedef structure for the general purpose communication library. We have to initialize the member of this structure as per the communication protocol types.

For Example,

initialize the structure variables for TCP/IP communication


initialize the structure variables for UDP communication


Suppose, now we need to communicate with the server using the TCP/IP, then just called the CreateTcpComm function in your application.

For Example,

In this way, a single type could be used for UDP, SNA, and RS232 with exactly the same interface.


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

In callback function

In device drivers and low-level programming,  function pointer frequently uses in structure. A lot of call back functions are used in case of windows driver. This callback function is registered using the structure of function pointer in the driver for the simplicity and bug-free coding.

For Example,

In the case of PNP device, there is an AdDevice call-back function which is called by PnP manager. This call-back function is used to create a device object for the driver and registered all PNP call-back function using structure-function pointers.

Your opinion matters

Although here I have tried to discuss a lot of points regarding the function pointer in structure but I would like to know your opinion on the function pointer in structure. So please don’t forget to write a comment in the comment box.


Leave a Reply