Thread: practical use of function pointer

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    89

    practical use of function pointer

    I understand that function pointer is pointer in c language – that can store the address of a function, and can be use in call back

    simple code

    Code:
     #include <stdio.h>
    
    void foo()
    {
    	printf(" Welcome ");
    }
    
    
    
    
    int main() 
    {
        
    	void (*fp)() = &foo;
        // foo(); 
    	fp();
        return 0;
    }
    if i use function pointer or function name I get same result



    Could you please provide some clarity on why and when function pointers are commonly used ?

    I'd appreciate any examples you could offer to help me grasp their usefulness ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    qsort, qsort_s - cppreference.com is a prime example right there in the standard library.

    GUI's are often implemented using callback functions - which are function pointers.

    Threads are created using function pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    139
    A function pointer is used to provide a program with a way to change the function that will be called without having to use control flow.

    It is possible to handle most (but not all) dispatch tasks by a long series of if/else-if statements. (or switch, etc.) But not all. And not quickly or efficiently.

    The concept of "virtual dispatch" in C++ is based on a table of function pointers.

    Many of the GoF Behavior Patterns are based on function pointers, or can be efficiently implemented in C using function pointers. (Specifically Command and Strategy come to mind, but I think some of the others also use a pointer one way or another.)

    When you do "dynamic loading" of shared libraries/dlls, you are installing a function that did not exist as far as the compiler was concerned. So you have no name for the function, and no way of establishing linkage other than via the OS provided library. A function pointer is really the only mechanism available for communicating with code that was installed this way.

    @Salem has pointed out a few other ways to use them.

  4. #4
    Registered User
    Join Date
    Feb 2024
    Posts
    1
    Quote Originally Posted by Kittu20 View Post
    I understand that function pointer is pointer in c language – that can store the address of a function, and can be use in call back

    simple code

    Code:
     #include <stdio.h>
    
    void foo()
    {
        printf(" Welcome ");
    }
    
    
    
    
    int main() 
    {
        
        void (*fp)() = &foo;
        // foo(); 
        fp();
        return 0;
    }
    if i use function pointer or function name I get same result



    Could you please provide some clarity on why and when function pointers are commonly used ?

    I'd appreciate any examples you could offer to help me grasp their usefulness ?
    There are many quite good use cases in C for this.

    One is a finite state machine implementation where we have a 2D array of functions pointers (pre initialized) and our code can access function[X][Y] using the state and an event ID.

    Another is to simulate namespaces which C does not support, here we can create struct typedefs where the members are function pointers and we can then treat the struct as a namespace with "member" functions, see this example from code I created for an MCU last year.
    Last edited by Captain Kernel; 02-09-2024 at 05:07 PM.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    114
    Quote Originally Posted by Kittu20 View Post
    Could you please provide some clarity on why and when function pointers are commonly used ?

    I'd appreciate any examples you could offer to help me grasp their usefulness ?
    I wrote my "entertainment system" in C as a way to relax. It has been morphing over the past 12 years. My "entertainment system" can offer me TV Shows, Movies, Games, Images and Music. In all instances, which ever main selection I choose (Music, Games...), goes to my menu display. My menu display shows the possible values for each of the main headings. Once I choose a value, it executes the function pointer associated with the main heading.

    The reason is because my menu display is stupid, it doesn't care what it is displaying, or where is goes after selection. So I can use one smaller function to display the various types, and not need to constantly test where to go after selection. Because mpg123 doesn't know how to show an image file file and vlc doesn't know how to play a game file.

    It really makes for cleaner code. And for the record, it was something I had to go back and add, as it wasn't that obvious in the beginning when my "entertainment system" only consisted of 'Movies' and 'Music'.

    Code:
    typedef struct _menu_item
    {
        int type;
        char path[PATH_SIZE + 1];
        char name[NAME_SIZE + 1];
        void (*functionPtr) (struct _menu_item *, int);
    } MENU_ITEM;
    Last edited by Yonut; 02-09-2024 at 06:46 PM.

  6. #6
    Registered User
    Join Date
    Jan 2024
    Posts
    3
    a look into the functions "GetProcAddress" on windows or "dlsym" on linux, should give some insight into their usefulness in a real world example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Practical Programming
    By TwoBabyJedis in forum Programming Book and Product Reviews
    Replies: 3
    Last Post: 08-23-2011, 06:00 PM
  2. A practical recursive function, finally!
    By KenJackson in forum C Programming
    Replies: 7
    Last Post: 11-17-2009, 09:02 AM
  3. practical programming
    By IXxAlnxXI in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2008, 08:06 AM
  4. A practical help
    By Ducky in forum C++ Programming
    Replies: 9
    Last Post: 12-29-2007, 10:18 AM
  5. making it practical
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2002, 10:51 AM

Tags for this Thread