简体   繁体   English

在C中实现回调函数

[英]Implementing callback functions in C

I am a newbie to C. I am trying to implement callback function using function pointers. 我是C的新手。我正在尝试使用函数指针实现回调函数。

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? 这是什么错误?有人可以帮忙吗?

  1. register is a C keyword: Use another name for the function. register是一个C关键字:为该函数使用另一个名称。

  2. You have extra parantheses around the callback parameter. 你在回调参数周围有额外的parantheses。 It should be: 它应该是:

     void funcName(void (*callback) (void)) 

I would recommend to use a typedef 我建议使用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 编辑:删除了注册问题

您不能将'register'用作函数名,因为它是C关键字。

2 problems: 2个问题:

  • you can't use the name register as it's a keyword (not used often anymore, but it's still there) 你不能使用名称register因为它是一个关键字(不再经常使用,但它仍然存在)
  • 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. (摆脱参数声明周围的parens。

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: 另外,您可能会收到有关callback_proc()没有与callback变量匹配的延迟的警告(取决于您如何编译程序 - 如C或C ++),因此您可能希望将其声明更改为:

void callback_proc (void)

to make it explicit that it takes no parameters. 明确表示它不需要参数。

Have a look at type safe callbacks from ccan . 看看来自ccan的 类型安全回调 Its one thing to expose a typed function pointer for the world to use, its another to ensure sane casting. 它为暴露一个类型化的函数指针供世界使用,它的另一个东西是确保理智的投射。

#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). 声明the_callback static会更有意义,如果这个代码是模块化的,那么你将被迫调用callback_register来设置它,并且callback以便调用它 - 在实现(.c)之外不能访问the_callback只有函数声明将在标题(.h)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM