Pointer to a function pointer

This is a discussion on Pointer to a function pointer within the C Programming forums, part of the General Programming Boards category; hi, Can anyone show me.....how to create a pointer to a function pointer. Please show me by creating some functions.... ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    17

    Pointer to a function pointer

    hi,

    Can anyone show me.....how to create a pointer to a function pointer.
    Please show me by creating some functions....

    Also

    What this apply.....

    char (*(x()) [ ]) ()

    I know it is function returning a pointer to array of pointer to functions returning char....

    But please explain me with some functions....or code samples....

    Regards,
    R

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You mean you couldn't find the entire website dedicated to function pointers!?!

    http://www.function-pointer.org/

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    17

    fnction pointer

    The site has examples of function pointers,function returning function pointers.

    But there is no examples for

    Function returning an pointer to array of function pointers.

    pointer to a function pointer.

    function pointer returning a function pointer etc....


    I will be happy if someone explains the above cases with simple examples.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Simple examples and "function returning array" is an oxymoron - there is no such thing.

    Generally, we don't want to return an array from a C function.

    A workaround would be to store the fuinction pointers in a struct, and return the (address of the) struct. E.g.
    Code:
    #define SIZE 3
    struct myFuncPtrs
    {
        char (*funcPtrs)[SIZE];
    };
    
    
    
    myFuncPtrs *fetchFunctions(int arg)
    {
       static struct myFuncPtrs type1 = { ... };
       static struct myFuncPtrs type2 = { ... };  
    
       if (arg) 
          return &type1;
        else
          return &type2;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    17

    function pointer

    Please understand the question properly.......

    I am asking for an example like,

    Function returning a pointer to an array of function pointers.....not the entire array.

    Also examples for....

    pointer to a function pointer,

    function pointer returning function pointer.

    Regards,
    R

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    zacs7 linked to a website. Have you visited it?
    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

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    It's quite easy with typedefs.
    First one for a function pointer:
    Code:
    typedef void (fptr)(int, float, double/*, whatever*/);
    
    fptr* p = &foo; /* This is a function pointer */
    fptr* p2 = &foo2;
    fptr* p3 = &foo3;
    
    fptr* array[] = { p, p2, p3 };
    And so on.
    I didn't test it, but it should hopefully work!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Here is an example of a function returning a pointer to a function pointer [actually, a pointer to an array of function pointers]. Using typedef makes it much more manageable to do this.

    Code:
    #include <stdio.h>
    
    typedef void (*funcptr)(void);
    
    void f1(void);
    void f2(void);
    
    void f1()
    {
            printf("Iam in f1 \n");
    }
    
    void f2()
    {
            printf("Iam in f2 \n");
    }
    
    
    funcptr *fptRet()
    {
      static funcptr arrFunPtr[2];
    
      arrFunPtr[0] = f1;
      arrFunPtr[1] = f2;
      
      return arrFunPtr;
    }
    
    
    int main()
    {
      funcptr *pfptr;
      pfptr = fptRet();
      if (arrFunPtr != pfptr)
        printf("Didn't work\n");
      pfptr[1]();
      return 0;
    }
    I wouldn't recommend doing this sort of convuluted solution unless you have a really good reason, but that is a different story, I suppose.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    17

    function pointer

    hi,

    Please don't use typedef in defining a function pointer,its confusing.I have already said that the site did not contain information like.

    Pointer to a array of function pointer;

    I know what is a function pointer:

    char (*fun) (); //It is function pointer with function returning char

    char (*fun[5]) (); //Array of 5 function pointer with functions returning char.

    char ((*fun)()) (); //function returning a function pointer

    But i dont know.

    How to create a pointer to a function pointer.????

    How to create a pointer to an array of function pointers.???

    How to create function pointer returning a function pointer ???

    Regards,
    Ram

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    Learn how to interpret function pointer typedefs and everything will become so much clearer. It simplifies syntax enormously.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you insist on not listening to good advice, that is your choice. The syntax for function pointers is awkward. Trying to make it more awkward by using obscure things like function pointers returning pointers to function pointers without typedefs is lunacy - no sane programmer will want to read such code, and no sane programmer will want to write such. So stop asking for something no one wants to use.

    typedefs is an EXCELLENT way to avoid all the hairy parenthesis that you will spend half a day figuring out which one belongs to what [it gets even better when you have several different arguments to the functions].

    Have you considered why you don't actually find the solutions you are asking for?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can't expect to find an example for every possible use of function pointers, you might actually have to learn how they work. Then apply the idea yourself, and I don't mean that in a harsh way.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by matsp
    Here is an example of a function returning a pointer to a function pointer [actually, a pointer to an array of function pointers].
    The good (or perhaps bad) old "array decays to a pointer to its first element" thing still applies here, so it really is a function returning a pointer to a function pointer

    Quote Originally Posted by rohit_second
    Please don't use typedef in defining a function pointer,its confusing.
    Personally, I find not using typedef for a function pointer even more confusing than using it.
    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

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    typedef is teh standard way of implimenting function pointers.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  15. #15
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Quote Originally Posted by rohit_second View Post
    Please understand the question properly.......

    I am asking for an example like,

    Function returning a pointer to an array of function pointers.....not the entire array.

    Also examples for....

    pointer to a function pointer,

    function pointer returning function pointer.

    Regards,
    R
    Here ya go friend, I guess this is what you wanted. Have a nice day!

    Code:
    #include <stdio.h>
    
    typedef char (*(*ComplexFuncPtrType)[5])(char);
    
    
    char SomeFunc1(char a)
    {
       return a;
    }
    
    
    //Here is the same function as below only it uses typedef instead
    /*
    ComplexFuncPtrType Func(ComplexFuncPtrType cfptVar)
    { 
       (*cfptVar)[0]=SomeFunc1;
        return cfptVar;  
    }
    */
    
    
    //6   4 2  1    PARAM    5   3        1         2    4     PARAM     3     5     
    char (*(*Func(/*PARAM*/char (*(*ComplexFuncPtr)[5])(char)/*PARAM*/))[5])(char)
    {    
      (*ComplexFuncPtr)[0]=SomeFunc1;
       return ComplexFuncPtr;
    }
    /*
    PARAMETER of Func()
    --------------------
    (1)pointer, named ComplexFuncPtr, to
    (2)an array of 5
    (3)pointers to
    (4)functions with char as parameter
    (5)which return a char
    
    Func()
    ----
    (1)Function named Func
    (2)which returns a pointer to
    (3)an array of 5
    (4)pointers to
    (5)functions with char as parameter
    (6)which return a char
    */
    
    int main()
    {
        char (*FuncArray[5])(char)={0};  //Array of 5 function pointers
        char (*(*pFuncArray)[5])(char)={0};  //Pointer to an array of 5 function pointers
        //FuncArray[0]=SomeFunc1;  //There are different paths in which to assign SomeFunc1
        pFuncArray = (ComplexFuncPtrType)Func((ComplexFuncPtrType) FuncArray);
        printf("&#37;c", (*(*pFuncArray)[0])('A') );
      
       /////////////////////MORE EXAMPLES////////////////////////////////////////////////
        int (*Funcptr)()=NULL;
        int (**ptrToFuncptr)()=&Funcptr;  //Pointer to a function pointer.
    
        int (*(*FuncptrReturn)())  ()=NULL;  //Pointer to a function returning a pointer to a function.
        getchar();
       
        return 0;
    }
    Last edited by JimmyJones; 08-29-2008 at 03:45 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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