Thread: declaring function pointer types for callbacks

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    36

    declaring function pointer types for callbacks

    Hi all,

    I am implementing some events (callbacks), and have come across a problem that I daresay has a simple solution.

    The problem is that one of the inputs into my callback function is a struct that I declare later on, and it doesn't like it because the struct doesn't exist yet. The problem is that I can't move the declaration of the struct above the declaration of the callback function either, because the struct itself contains one of these callbacks!!

    example:
    Code:
    typedef void (*eGPD_onButtonPress)( tGPD_widget, unsigned short, unsigned short, char, char);
    typedef void (*eGPD_onMouseIn)( tGPD_widget );
    typedef void (*eGPD_onMouseOut)( tGPD_widget );
    
    typedef struct tGPD_widget{
    	char type;
    	void *object;
    	unsigned short x;
    	unsigned short y;
    	tGPD_list *children;
    	struct tGPD_widget *parent;
    	char redraw;
    	eGPD_onMouseIn onMouseIn;
    	eGPD_onMouseOut onMouseOut; 
    }tGPD_widget;
    As you can see, tGPD_widget contains two events/callbacks (so far) which also have tGPD_widgets as inputs.

    How do I properly define these?

    EDIT: If I remove the references to tGPD_widget from the callback definitions, it compiles and runs ok - however i do want the tGPD_widget input in there!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,928
    Use a forward declaration and pass a pointer to struct tGPD_widget to the callbacks.
    Code:
    struct tGPD_widget; /* forware decl. */
    
    typedef void (*eGPD_onButtonPress)( struct tGPD_widget*, unsigned short, unsigned short, char, char);
    typedef void (*eGPD_onMouseIn)( struct tGPD_widget* );
    typedef void (*eGPD_onMouseOut)( struct tGPD_widget* );
    
    typedef struct tGPD_widget{
        char type;
        void *object;
        unsigned short x;
        unsigned short y;
        tGPD_list *children;
        struct tGPD_widget *parent;
        char redraw;
        eGPD_onMouseIn onMouseIn;
        eGPD_onMouseOut onMouseOut; 
    }tGPD_widget;
    gg

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    36
    Thanks for the quick reply.

    Firstly, sorry - tGPD_widget* (pointer to tGPD_widget) is what I originally intended to be passed in, not the struct itself.

    Secondly, it compiles now (by adding the 'struct' keyword) but gives me HEAPS of warnings like this:

    Code:
    gpd_windows.h:18: warning: "struct tGPD_widget" declared inside parameter list
    gpd_windows.h:18: warning: its scope is only this definition or declaration, which is probably not what you want
    Is there any way to do this without the warnings?

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    36
    I did it like this:
    Code:
    typedef struct tGPD_widget* pGPD_widget;
    typedef void (*eGPD_onButtonPress)( pGPD_widget, unsigned short, unsigned short, char, char);
    typedef void (*eGPD_onMouseIn)( pGPD_widget );
    typedef void (*eGPD_onMouseOut)( pGPD_widget );
    And it compiles ok. Is this 'normal' syntax?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,928
    There's nothing wrong with what I posted...

    Let's see gpd_windows.h - atleast up to the tGPD_widget struct declaration (which, I'm assuming, includes line 18).

    gg

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,928
    >> And it compiles ok. Is this 'normal' syntax?
    It isn't any different from what I posted except you typedef'd "struct tGPD_widget*" to "pGPD_widget" and used the typedef instead.

    gg

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    36
    Correct, but it got rid of my compiler warnings.

    Thanks for your help!

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. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 08:39 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM