6

I am a newbie to C. I am trying to implement callback function using function pointers.

I am getting an error

:test_callback.c:10: error: expected identifier or ‘(’ before ‘void’

when I try to compile the following program:

#include<stdio.h>

void (*callback) (void);

void callback_proc ()
{
  printf ("Inside callback function\n");
}

void register ((void (*callback) (void)))
{
  printf ("Inside registration \n");
  callback (); /* Calling an initial callback with function pointer */
}

int main ()
{
  callback = callback_proc;/* Assigning function to the function pointer */
  register (callback);/* Passing the function pointer */
  return 0;
}

What is this error?Can anyone help?

4
  • register is a keyword, BTW. Commented Apr 29, 2010 at 15:19
  • What line do you get the error on? Commented Apr 29, 2010 at 15:19
  • Just a tip: typedefs help make things a little more readable: typedef void (*CallbackFunc)(void);. Then your function signature is void registerFunc(CallbackFunc callback) and your declarations are CallbackFunc my_cb = &callback_proc Commented Apr 29, 2010 at 15:28
  • Oh, one more thing: void callback_proc () is NOT THE SAME as void callback_proc (void) in C. Commented Apr 29, 2010 at 15:28

6 Answers 6

19
  1. register is a C keyword: Use another name for the function.

  2. You have extra parantheses around the callback parameter. It should be:

    void funcName(void (*callback) (void))
    
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, the extra parens was hard to catch. +1
3

I would recommend to use a typedef

#include<stdio.h>

typedef void (*callback_t) (void);
callback_t callback;

void callback_proc(void)
{
    printf ("Inside callback function\n");
}

void reg( callback_t _callback )
{
    printf ("Inside registration \n");
    _callback();
}

int main ()
{
    callback = callback_proc;
    reg(callback);

    return 0;
}

EDIT: removed the register issue

3 Comments

where is the typedef ? that's a very good recommendation, but your example is missing it.
I fail to see the typedef? What you've done is defined a function pointer, which is not type safe.
Sorry didn't realize that I had a problem with copy and paste
2

You can't use 'register' as a function name as it's a C keyword.

Comments

0

2 problems:

  • you can't use the name register as it's a keyword (not used often anymore, but it's still there)
  • change the definition of the function from

    void wasRegister((void (*callback) (void)))
    

    to:

    void wasRegister(void (*callback) (void))
    

    (get rid of the parens around the parameter's declaration.

Also you might get a warning about callback_proc() not having a matching delaration to the callback variable (depending on how you compile the program - as C or C++), so you might want to change its declaration to:

void callback_proc (void)

to make it explicit that it takes no parameters.

Comments

0

Have a look at type safe callbacks from ccan. Its one thing to expose a typed function pointer for the world to use, its another to ensure sane casting.

2 Comments

Hi Tim, the link in your answer is dead.
@bummi, if Tim ever decides to delete this answer, He'll get a message This post was deleted by Tim Post♦. To know why your answer was deleted please use meta :D
0
#include<stdio.h>

typedef void (*callback_func) (void);

static callback_func the_callback = 0;

void process (void)
{
  printf ("Inside process function\n");
}

void callback_register (callback_func cb)
{
  the_callback = cb;
  printf ("Inside registration \n");
}

void callback(void)
{
    the_callback();
}

int main (void)
{
  callback_register(process); /* Passing the function pointer */
  callback();
  return 0;
}

Declaring the_callback static would make more sense if this code was modularized and then you would be forced to call callback_register in order to set it, and callback in order to call it - the_callback would not be accessible outside of the implementation (.c) only the function declarations would be in the header (.h).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.