Doing a linked-list of function pointers

This is a discussion on Doing a linked-list of function pointers within the C Programming forums, part of the General Programming Boards category; Hi all - I'm wanting to do a linked-list of function pointers. The functions will have different return types and ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    15

    Doing a linked-list of function pointers

    Hi all -

    I'm wanting to do a linked-list of function pointers. The functions will have different return types and different numbers of arguments.

    I'm unsure as to whether this is even possible.
    What I need is basically a generic "function-pointer signature" in the bit of code below.
    It would probably use "void *" but more than that I don't know.

    Code:
    /*  Create a linked-list of function pointers.  */ 
    typedef struct fp { 
        void * funcptr;  /*  This is where I'm stuck. */ 
        struct fp *next;
    } fp_list;
    Many thanks in advance for your help!
    - Andy

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    This should suffice as a generic function pointer if that's what you are asking.
    Code:
    typedef void (*voidFunc)(void);
    Of course if you want to store a function pointer to something with a different signature, and cast back to what it should be before calling it, then I would hope that you just use the right function signature to begin with. Perhaps consider making all of your functions have the same signature, e.g. the parameters could be an array of some kind of variant structure. I dunno, it would really help to know what you are doing as this is a really strange thing to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > The functions will have different return types and different numbers of arguments.
    You're going to have to have some kind of flag indicating the type, otherwise how will you cast it back to the right type to call it?
    Code:
    typedef struct fp {
        enum { ONE, TWO } type;
        union {
            int (*one)(void);
            void (*two)(int);
        } fn;
        struct fp *next;
    } fp_list;
    Your call list traversal would just have a switch/case to use the correct union member.
    Code:
    switch ( p->type ) {
        case ONE: result = p->fn.one(); break;
        case TWO: ....
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Hi again -
    Thanks very much for your solutions, iMalc and Salem! Very useful!

    The "use case" for this is that I want to poke around with a simple "object system". The linked-list of function pointers will be the methods that an object has.
    ( If there is a better way of doing this, then I'm very keen to hear about it. )

    ( I should say that I'm doing this in C rather than C++ because I much prefer the simpler, cleaner syntax of C. )

    Anyway, I reckon that the code you've given here should be fine for getting me on my way with this.
    Many thanks again - bye for now -
    - Andy (latte123)
    Last edited by latte123; 12-21-2012 at 11:47 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    I would not use a linked-list of function pointers. An Array of function pointers might be best. And if in fact you are just emmulating OOP then I'd suggest doing it the C++ way where you know the actual function signature of each method, and you don't have an array per object, but an array per type of object. Really, try and understand how C++ compilers do it internally, as the problem has had plenty of prior work done on it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Quote Originally Posted by iMalc View Post
    I would not use a linked-list of function pointers. An Array of function pointers might be best. And if in fact you are just emmulating OOP then I'd suggest doing it the C++ way where you know the actual function signature of each method, and you don't have an array per object, but an array per type of object. Really, try and understand how C++ compilers do it internally, as the problem has had plenty of prior work done on it.
    Hi iMalc, thanks very much for that feedback.
    I agree - that's a much more systematic way of doing things.
    Thanks again - this thread can be marked as SOLVED.
    - latte123

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List with Two Pointers
    By aspekt9 in forum C Programming
    Replies: 5
    Last Post: 12-07-2009, 03:23 PM
  2. Replies: 7
    Last Post: 10-09-2009, 06:27 AM
  3. Linked List, pointers, function question
    By Canucklehead in forum C++ Programming
    Replies: 9
    Last Post: 11-02-2005, 12:10 PM
  4. linked list and pointers : ??????
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 05-04-2005, 05:02 AM
  5. Linked List and Array of Pointers
    By Nish in forum C Programming
    Replies: 4
    Last Post: 03-04-2005, 03:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21