Question on Function Pointers

This is a discussion on Question on Function Pointers within the C Programming forums, part of the General Programming Boards category; Hello, I was looking on using function pointers in my code and it would be great if you guys can ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Question on Function Pointers

    Hello,

    I was looking on using function pointers in my code and it would be great if you guys can point me in the right direction or give hints.

    I basically want to know how to invoke different functions ( functions with different arg numbers and return type) using a single function pointer.

    example

    Code:
    void func1();
    
    int func2(int,float);
    
    main()
    {
        void (*funcptr) ();
    
       functptr = & func1
       funcptr();
    
       funcptr = & func2  // i know that i need a different funcptr here, but is there a way i can type cast or something?
    
      funcptr(2,2.5);
    
    return;
    
    }
    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by rebelstar
    I basically want to know how to invoke different functions ( functions with different arg numbers and return type) using a single function pointer.
    I do not think you can do that, at least not within the realm of well defined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Thanks for the reply

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Function Pointers Tutorial index. Take a look there, should have all the information you need and more.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    i know that i need a different funcptr here, but is there a way i can type cast or something?
    Sure, you can use a cast to convert one function type into your "generic" function pointer, and then cast it back to the original type and it's guaranteed to work. This gains you nothing, however, because you have to know the type of the function you're converting at compile-time, not run-time, so you may as well just create a proper type.

    You can sort of achieve what you want with a lot of hacking:
    Code:
    struct function_pointer
    {
      enum { VOID_VOID, VOID_INT, VOID_INTP } type;
      void (*fp)(void);
    };
    #define MAKE_FP(fp) ( (void(*)(void))(fp) )
    
    void x(void);
    void y(int);
    void z(int *);
    
    struct function_pointer fp;
    fp.type = VOID_VOID; fp.fp = MAKE_FP(x);
    fp.type = VOID_INT; fp.fp = MAKE_FP(y);
    fp.type = VOID_INTP; fp.fp = MAKE_FP(z);
    
    switch(fp.type)
    {
      case VOID_VOID:
        fp.fp();
        break;
      case VOID_INT:
        ((void(*)(int))fp.fp)(10);
        break;
      case VOID_INTP:
        ((void(*)(int *))fp.fp)(NULL);
        break;
    }
    But all this is good at demonstrating, I think, is that C is not exactly Haskell when it comes to polymorphism.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Thanks for the hint cas and thanks Andrew.
    This solution is pretty good,.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to function question
    By -EquinoX- in forum C Programming
    Replies: 31
    Last Post: 04-29-2008, 01:06 PM
  2. Question about my trim function and pointers
    By space-oddity in forum C Programming
    Replies: 10
    Last Post: 04-28-2008, 01:22 AM
  3. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 08:21 PM
  4. Linked List, pointers, function question
    By Canucklehead in forum C++ Programming
    Replies: 9
    Last Post: 11-02-2005, 12:10 PM
  5. Replies: 5
    Last Post: 02-20-2004, 08:36 AM

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