Thread: static array of function pointers.

  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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't seem to have a label in the code that you showed, so I do not know what is the problem. You do seem to have a typo error here though:
    Code:
    assert ((index < MAX_CALLBACKS);
    That is, it should be:
    Code:
    assert (index < MAX_CALLBACKS);
    I suggest that you post the smallest and simplest program that you expect should compile but which results in this compile error. For example, this program should demonstrate the error:
    Code:
    int main(void)
    {
    my_label:
        int n;
        return 0;
    }
    As you can see, my_label is a label for the declaration of n, but "a label can only be part of a statement and a declaration is not a statement".
    Last edited by laserlight; 01-29-2015 at 12:56 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by laserlight View Post
    You don't seem to have a label in the code that you showed, so I do not know what is the problem. You do seem to have a typo error here though:
    Code:
    assert ((index < MAX_CALLBACKS);
    That is, it should be:
    Code:
    assert (index < MAX_CALLBACKS);
    I suggest that you post the smallest and simplest program that you expect should compile but which results in this compile error. For example, this program should demonstrate the error:
    Code:
    int main(void)
    {
    my_label:
        int n;
        return 0;
    }
    As you can see, my_label is a label for the declaration of n, but "a label can only be part of a statement and a declaration is not a statement".
    Sorry, that was my mistake. I've fixed that.

    I know I don't have a label so I'm confused why it's saying that error.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dayalsoap
    I know I don't have a label so I'm confused why it's saying that error.
    Then you really have to post a program that we can compile and see for ourselves. Otherwise, based on the error message and the code that you have shown, I am as stuck as you are.

    Or at the very least post gmanager_client.c from lines 30+ to 50, indicating where exactly is line 43.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by laserlight View Post
    Then you really have to post a program that we can compile and see for ourselves. Otherwise, based on the error message and the code that you have shown, I am as stuck as you are.

    Or at the very least post gmanager_client.c from lines 30+ to 50, indicating where exactly is line 43.
    Okay, so it turns out I DID have a label: I didn't realize that the case statement of a switch counted as a label. C has a requirement that you can't have declarations directly after labels. I just had to do some light restructuring to avoid this and it compiles and works.

    Thanks.

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