简体   繁体   English

c 中的回调和 c 中的指针

[英]Callbacks in c and pointers in c

I have a task to make a function that summarize array and list elements(int type) with callbacks using 2 given signatures:我的任务是制作一个 function ,它使用 2 个给定的签名通过回调汇总数组和列表元素(int 类型):

typedef void (* callback )( void * ctx , int * value );
void arrayForeach ( void * ctx , callback func , int * arr , int n );

I don't know how I can use function pointers here so I have no idea what to do.我不知道如何在这里使用 function 指针,所以我不知道该怎么做。 Can write this simple code for me and explain what is callback, please.请帮我写这个简单的代码并解释什么是回调。

/*  caller must check if arr[0] to arr[n-1] is a valid array or part of an array */
void arrayForEach(int ctx, callback func, int * arr, int n)
{
    for (int i = 0; i < n; i++)
    {
        func(ctx, arr[i]);
        /* you could also use (*func)(ctx, arr[i]); */
    }
}

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

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