Thread: pointers to function question

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    yes. a pointer to a function returning an int and taking two void pointers as parameters.

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so can have a type that returns something and takes something as the parameter? this seems weird to me..

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    no, the type is a pointer. the type that the pointer points to is a function. that function is a type that returns an int and takes two void *s as parameters.
    Code:
    #include <stdio.h>
    
    typedef int (*COMPAREFUNC)(void*, void*);
    
    /* ^
     * |Notice the similarity in these two signatures.
     * |Same return type, same type and number of arguments.
     * v
     */
    
    int func(void*param1,void*param2);
    
    int main()
    {
            COMPAREFUNC my_func_pointer;
            char a=1,b=0,c=0,d=0;
    
            func(&a,&b); /* prints "in func" */
            my_func_pointer = func; /* my_func_pointer now points to func */
            my_func_pointer(&c,&d); /* prints "in func" */
    
            return 0;
    }
    
    int func(void *param1, void *param2)
    {
            puts("in func");
            return 0;
    }
    Last edited by robwhit; 04-28-2008 at 06:49 PM. Reason: add code

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay so we can do a typedef for such thing? not only for int, float, etc.. but also something that takes a parameter and returns something?

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    well, you can do it for a /pointer/ to a function (or a pointer to a pointer to a function or...), but I don't think you can do it for a function.

    see my edit in my last message for an example.

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yeah I understand your example... but yet still I am not still sure somehow

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Wow, talk about complicated stuff. I'll save this thread to my hard drive. I never know when I could use answers to an extremely intelligent question like this one

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by -EquinoX- View Post
    yeah I understand your example... but yet still I am not still sure somehow
    What is it specifically you don't understand?

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The typedef simply means you can write this

    COMPAREFUNC my_func_pointer;

    As opposed to this
    int (*my_func_pointer)(void*, void*);

    One you can live with, but the syntax is heavy on the parentheses and verbose with all the parameter types, which can be hard on the fingers if you need lots of them. The typedef saves you from all that.


    Since no one has mentioned it yet, http://www.newty.de/fpt/index.html
    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.

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    maybe I am just not used to the typedef that can take parameters..that's all my confusion is

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by -EquinoX- View Post
    maybe I am just not used to the typedef that can take parameters..that's all my confusion is
    The typedef doesn't take parameters. However, the type itself is a function pointer, and part of a function pointer is the arguments to the function. Yes, it takes a bit to get used to. If I haven't used function pointers for a bit, I have to look up and/or think about where all the parenthesis and stars belong.

    Here's one that takes a bit of thinking.
    Code:
    typedef int *(*funcptr)(int (*func1)(void));
    It does get a bit simpler if you do two typedefs:
    Code:
    typedef int (*subfunc)(void);
    typedef int *(*funcptr)(subfunc func1);
    That's a function pointer (called funcptr) that returns a pointer to int, and takes a function pointer that returns int, which takes no arguments.

    --
    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. #27
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Function pointers are obscure, but when you're used to them then you'll see the power :-)
    Code:
    static unsigned char (*const GpsHandler[NUM_FUNS][NUM_TPS])(void (*GpsMsgs)(void)) = {...};
    Is and array of function pointers where functions return unsigned char and have an argument which is function pointer which returns void and has void as an argument.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To add another thing into the mix. I usually do:

    typedef int (COMPAREFUNC)(void*, void*);
    (As opposed to
    typedef int (*COMPAREFUNC)(void*, void*);
    Note the missing *)

    So that when I define a function pointer, I can do:

    COMPAREFUNC* pFnc;

    It's now easier to see it's actually a pointer.
    Now that it's bad to put the * in the type, but I prefer the second way. Either one is possible and both just as good.
    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.

  14. #29
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    thanks guys for helping me.. can you give me some other typedef examples of a function pointer

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typical Windows API:

    Code:
    typedef int bool;
    #define true 1
    #define false 0
    
    bool callback(int id)
    {
        printf("%i\n");
        return true;
    }
    
    typedef bool (callbackptr)(int);
    
    void example(callbackptr* pcallback)
    {
        int i = 0;
        for (; i < 100; i++) 
        {
            if (! pcallback(i) ) /* If function returns false, then break the loop */
                break;
        }
    }
    
    int main(void)
    {
        example(&callback);
        return 0;
    }
    Will print 1 - 100 (or should). The callback function can cancel by returning false instead of true.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM