Search code examples
cfunctionparametersfunction-pointersfunction-parameter

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?


Solution

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

    void work(void) {
    ...;
    }
    
    void main(void) {
    task_create(work, void, void);
    }