Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: is it possible to hold different types of function pointers in a table(structure)

  1. #1
    Join Date
    Aug 2012
    Beans
    544

    is it possible to hold different types of function pointers in a table(structure)

    Hello,
    I want a design like

    Code:
    typedef struct _LOOKUP
    {
     char* FunctionSignature;
      DONTKNOW FunctionPointer;
    }LOOKUP;
    And I want to populate it something like,

    LOOKUP* ptr;
    ptr->FunctionSignature = "iic";//which is code for int fun(int, char);
    In that case, ptr->FunctionPointer = int (*fptr)(int, char);

    Thanks.

    PS : Assume my requirement is that, I want to use a function pointer depending on the function supplied as input by a user, and I want to call this function using a FunctionPointer which has been maintained in the LOOKUP table.
    Last edited by IAMTubby; March 25th, 2013 at 06:06 PM.

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: is it possible to hold different types of function pointers in a table(structure)

    you can do that by using a union - so that you can use the correct type for your pointer when you store them.

    The only other option is to store everything as a (void *)(void) , and make sure you cast to the right type when you try to call it.

    C does NOT have dynamic typing.
    Last edited by Tony Flury; March 25th, 2013 at 11:14 PM.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Aug 2012
    Beans
    544

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by Tony Flury View Post
    The only other option is to store everything as a (void *)(void) , and make sure you cast to the right type when you try to call it.
    Tony Flury, I'm trying this out right now, and shall get back to you in a few minutes with the result. I'll have to do some googling about typecasting etc, not very comfortable with them. Please help me out after I have tried.

    you can do that by using a union - so that you can use the correct type for your pointer when you store them.
    I didn't get this, but it's cool, let me try the void pointer method first.

  4. #4
    Join Date
    Aug 2012
    Beans
    544

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by Tony Flury View Post
    The only other option is to store everything as a (void *)(void) , and make sure you cast to the right type when you try to call it.
    TonyFlury, check this out. Is is okay ?
    Basically, I am receiving the return from dlsym call as a void* and then typecasting it to type FPTR_REQ.(By REQ, I meant required)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dlfcn.h>
    
    
    float fun(int, char*);
    typedef void* (*FPTR_VOID)(void);
    typedef float (*FPTR_REQ)(int,char*);
    
    
    int main(void)
    {
     void* handle;
     char* error;
     FPTR_VOID fptr_void;
     FPTR_REQ fptr_req;
     float ret;
    
    
     handle = dlopen(NULL,RTLD_LAZY);
     if(!handle)
     {
      fprintf(stderr,"%s\n",dlerror());
      exit(1);
     }
     dlerror();
     fptr_void = dlsym(handle,"fun");
     if((error = dlerror()) != NULL)
     {
      fprintf(stderr,"%s\n",error);
      exit(1);
     }
      fptr_req = (FPTR_REQ)fptr_void;
     ret = (*fptr_req)(1,"hello");
    
    
     printf("ret == [%f]\n",ret);
    
    
     return 0;
    }
    
    
    float fun(int a, char* str)
    {
     printf("a == [%d]\n",a);
     printf("str == [%s]\n",str);
     return 3.14;
    }
    Build steps :
    gcc -rdynamic -o exe main.c -ldl
    Last edited by IAMTubby; March 26th, 2013 at 06:19 AM.

  5. #5
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: is it possible to hold different types of function pointers in a table(structure)

    what about functions which have a different call signature - you might want your proof of concept code to execute a number of different function signatures before you put this concept into production.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  6. #6
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: is it possible to hold different types of function pointers in a table(structure)

    Take a look at the first answer to this discussion thread: http://stackoverflow.com/questions/1...le-my-function

    Interestingly, it seems that the POSIX "approved" way (the horrible and infamous *(void **)(&ptr) cast) that you find on dlsym's manpage breaks the POSIX standard itself... :S (I haven't proven this, but I'll do it right now... Where's my compiler? )

  7. #7
    Join Date
    Aug 2012
    Beans
    544

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by Tony Flury View Post
    you might want your proof of concept code to execute a number of different function signatures before you put this concept into production.
    Sure Sir, I shall do that and get back in some time.

    But, just going back, ideally I would like to have a design where, I can call the function using the function pointer returned by dlsym irrespective of the signature of the function.Consider a scenario like this,

    "There are 4 functions defined in a program. Assume all 4 functions are of different signature. The program logic executes the correct function depending on the name of the function supplied as input by the user. Needless to say, I don't want to execute the function using an if-else construct like if(user says fun3){fun3();}, but do a dlsym for fun3 and execute using the function pointer returned. "

    Is is possible to achieve this ?

    Thanks.
    Last edited by IAMTubby; March 26th, 2013 at 11:55 AM.

  8. #8
    Join Date
    Aug 2012
    Beans
    544

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by nvteighen View Post
    Take a look at the first answer to this discussion thread: http://stackoverflow.com/questions/1...le-my-function

    Interestingly, it seems that the POSIX "approved" way (the horrible and infamous *(void **)(&ptr) cast) that you find on dlsym's manpage breaks the POSIX standard itself... :S (I haven't proven this, but I'll do it right now... Where's my compiler? )
    nvteighen, thank you so so much for telling that there could be a mistake there. That code had left me totally confused. To me(maybe my lack of knowledge, but, nevertheless), it was like instead of saying -5 you say, -(--5).
    I'll read the link and wait for your reply .

  9. #9

    Re: is it possible to hold different types of function pointers in a table(structure)

    If the functions have different signatures, you're going to have to do some kind of if-else construct anyway, in order to pass the correct arguments. No form of polymorphism or dynamic typing is possible in C unless you write it yourself.

    You're new to C and trying to do something that is not only unsafe, but needlessly complex and unnecessary. Stop. Learn to use the language properly first; make a text adventure or a program to manage your finances or something. Take a break and learn some assembly instead, if you're into that kind of thing. Then, if some day 10 years from now you find you really need to use dlsym, you won't be simultaneously trying to figure out calling conventions and stack frames and function pointer declarations and so forth.

    It's perfectly possible to write reusable, maintainable code in C, but to do so you'll have to abandon dynamic typing, polymorphism, overloading and other shortcuts that some languages do for you magically. Don't try to force them to work, because even if you succeed, you will only have made an unmaintainable mixture of C and non-C. And there are enough C++ programmers in the world already.

  10. #10
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by trent.josephsen View Post
    If the functions have different signatures, you're going to have to do some kind of if-else construct anyway, in order to pass the correct arguments. No form of polymorphism or dynamic typing is possible in C unless you write it yourself.

    You're new to C and trying to do something that is not only unsafe, but needlessly complex and unnecessary. Stop. Learn to use the language properly first; make a text adventure or a program to manage your finances or something. Take a break and learn some assembly instead, if you're into that kind of thing. Then, if some day 10 years from now you find you really need to use dlsym, you won't be simultaneously trying to figure out calling conventions and stack frames and function pointer declarations and so forth.

    It's perfectly possible to write reusable, maintainable code in C, but to do so you'll have to abandon dynamic typing, polymorphism, overloading and other shortcuts that some languages do for you magically. Don't try to force them to work, because even if you succeed, you will only have made an unmaintainable mixture of C and non-C. And there are enough C++ programmers in the world already.
    I wouldn't discourage anyone to research on these topics. If he's interested, great and endavant, as Catalans say! However, I agree with you that it's rather counterproductive to try to shoe-horn a homebrewed implementation of dynamic programming into any serious thing you're writing (defining "serious" as "something you care about, be it a hobby or a job"), as you'll be quickly frustrated or may lose a lot of worktime. If you need something like that, you'd either have to switch to a truly dynamic language (I'm excluding you, C++) or, if C is a requirement, use some hard-proven library like GLib or, better, combine C with another language.

    Don't set yourself any limits when learning programming, but be realistic on what your tools can do for you when you need them.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •