C Language

How to use the structure of function pointer in c language

aticleworld.com



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

In the C language, a pointer is like a variable but only there is one difference between the pointers and variables. A variable store value but pointer store address of the variable or other pointers.

Pointers 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 pointers in structure and assuming that you have good knowledge of pointers and you are aware from the function pointers.

How to declare function pointers in 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 pointers and these function pointers treat like as a member function in C++.

aticleworld

aticleworld

For example,

In below example, a structure contains some function pointers.



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

 

How to use function pointers in structure

In polymorphism

We already know, C is not an objective oriented language and it does not provide the concept ops. So when in a program, you intent 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 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 as 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.

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.

 

9 Comments

  1. Sanny Kumar Singh

    Hello Sir,
    Very nice article that is amazing.

  2. Bhudev Kumar

    very good article, it’s really helpful.

  3. Patil P M

    Good sir..these advanced concepts are very helpful..

  4. kiran

    Nice one

  5. Pramod

    Very good article
    I m following this page and most of the articles are really nice
    Good work

Leave a Reply

Theme by Anders Norén