A simple exmaple of CallBack function in C

本文通过一个简单例子介绍了如何在C语言中定义和使用回调函数。详细解释了如何将回调函数指针作为结构体成员,并在相关函数中调用该指针所指向的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here is a very rough example. Please note, the only thing I'm trying to demonstrate is the use of callbacks, its designed to be informational, not a demonstration.

Lets say that we have a library (or any set of functions that revolve around a structure), we're going to have code that looks similar to this (of course, I'm naming it foo):
typedef struct foo
{ int value;
  char *text;
} foo_t;

That's simple enough. We'd then (conventionally) provide some means of allocating and freeing it, such as:

foo_t *foo_start(void)
{
    foo_t *ret = NULL;

    ret = (foo_t *)malloc(sizeof(struct foo));
    if (ret == NULL)
        return NULL;

    return ret;
}

And then:

void foo_stop(foo_t *f)
{
   if (f != NULL)
     free(f);
}

But we want a callback, so we can define a function that will be entered when foo->text has something to report. To do that, we use a typed function pointer:

typedef void (* foo_callback_t)(int level, const char *data);

We also want any of the foo family of functions to be able to enter this callback conveniently. To do that, we need to add it to the structure, which would now look like this:

typedef struct foo {
     int value;
     char *text;
     foo_callback_t callback;
} foo_t;

Then we write the function that will actually be entered (using the same prototype of our callback type):

void my_foo_callback(int val, char *data)
{
    printf("Val is %d, data is %s\n", val, data == NULL ? "NULL" : data);
}

We then need to write some convenient way to say what function it actually points to:

void foo_reg_callback(foo_t *f, void *cbfunc)
{
    f->callback = cbfunc;
}

And then our other foo functions can use it, for instance:

int foo_bar(foo_t *f, char *data)
{
    if (data == NULL)
       f->callback(LOG_ERROR, "data was NULL");
}

Note that in the above:

f->callback(LOG_ERROR, "data was NULL");

Is just like doing this:

my_foo_callback(LOG_ERROR, "data was NULL"):

Except that, we enter my_foo_callback() via a function pointer that we previously set, thereby giving us the flexibility to define our own handler on the fly (and even switch handlers if / as needed).

One of the biggest problems with callbacks (and even the code above) is type safety when using them. A lot of callbacks will take a void * pointer, usually named something like context which could be any type of data/memory. This provides great flexibility, but can be problematic if your pointers get away from you. For instance, you don't want to accidentally cast what is actually a struct * as char * (orint for that matter) by assignment. You can pass much more than simple strings and integers - structures, unions, enums, etc can all be passed. CCAN's type safe callbacks help you to avoid unwittingly evil casts (to / from void *) when doing so.

Again, this is an over simplified example that's designed to give you an overview of one possible way to use callbacks. Please consider it psuedo code that is meant only as an example.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值