Thread: static array of function pointers.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    static array of function pointers.

    I basically have some code that lets users register callbacks into a callback table at a specified index. There is one element in this table for each event that can trigger a callback. I basically do something like this:

    In a header file
    Code:
    typedef struct _GMclient
    {
        GMcommunicator gcomm;
        GMclient_callback callback_table[(int)MAX_CALLBACKS];
    }GMclient;
    
    typedef int (*GMclient_callback)(GMclient*, void*, void*);
    Then I allow them to set the callback with a function that basically ends up doing this (in a .c file that includes the previous mentioned .h file):

    Code:
    void
    GMclient_register_callback(GMclient *client,
    			   GMclient_callback fxn,
    			   int index)
    {
        assert (client);
        assert (index < MAX_CALLBACKS);
        client->callback_table[index] = fxn;
        return;
    }

    Then later when I need to invoke that callback, I pull it out of the table

    Code:
    	    GMclient_callback *fxn = client->callback_table[CMD_INDEX];
    However, I get compilation errors:

    Code:
    src/gmanager_client.c:43:6: error: a label can only be part of a statement and a declaration is not a statement
    Any advice on what I'm doing wrong?
    Last edited by dayalsoap; 01-29-2015 at 12:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  2. Simulate the malloc function using a static array
    By thescratchy in forum C Programming
    Replies: 2
    Last Post: 11-12-2012, 07:05 PM
  3. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  4. What argument for a function? for static array variable
    By liechmaster in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 05:11 PM
  5. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 02:10 PM