Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a function as a parameter

void dispatch_for(dispatch_queue_t *queue, long number, void (* work)(long)){
    int loop = number;
    int i;
    task_t *ctask;
    for(i = 0; i<loop;i++){
        ctask = create_task((void *)work,number,"for_test");
        dispatch_async(queue,ctask);
    }
}    

task_t *task_create(void (* work)(void *), void *param, char* name){

    //do something
}

I'm getting work as a function and need to pass it to the function create_task..(1st parameter)
How should i pass it?

like image 853
Leanne Avatar asked Feb 23 '23 07:02

Leanne


2 Answers

Just use the identifier like any other parameter:

dispatch_for(queue, number, work);
like image 41
Karl Bielefeldt Avatar answered Feb 25 '23 20:02

Karl Bielefeldt


name of the function without the parentheses is the pointer to that function :

void work(void) {
...;
}

void main(void) {
task_create(work, void, void);
}
like image 195
Matthieu Avatar answered Feb 25 '23 19:02

Matthieu