12

I have a function int rt_task_start (RT_TASK *task, void(*task_func)(void *arg), void *arg) where in second argument i am passing a function with argument.

  1. When i only pass a function name at that time there is no problem.(as expected it's working). rt_task_start(&demo_task1, demo, 1);
  2. But when i pass rt_task_start(&demo_task1, demo(&val), 1); it's giving me error error: invalid use of void expression. Variable val is defined before. int val = 0;
  3. When i call with this rt_task_start(&demo_task1, demo(val), 1); this is showing error Warning passing argument 1 of 'demo' makes pointer from integer without a cast then error: invalid use of void expression.
  4. int *val; *val = 0; rt_task_start(&demo_task1, demo(&val), 1); this is also giving me error.

I can't understand what should i pass, as a void pointer. It's giving me error. Any Idea Please!

Arun Gupta
  • 780
  • 2
  • 7
  • 26
  • 2
    What is your intention with this function? Do you want the second argument to be a function pointer, or a different data type? It can't be both at the same time. Also, `int *val; *val = 0;` will probably create a segmentation fault since you're assigning a value to an undefined address location. – lurker Mar 29 '14 at 14:00
  • @mbratch thanx for pointing me out, i want second function only a function pointer. Actually i was confused about second argument `void(*task_func)(void *arg)` what type of parameters it can take. I though that i can send argument too, for case 2,3,4 which are incorrect at all. – Arun Gupta Mar 31 '14 at 06:08

3 Answers3

6
void (*task_func)(void *arg);

The above statement defines task_func to be a pointer to a function which takes a pointer of type void * and returns no value.

Therefore, when you call your function rt_task_start, you should pass a pointer to a function as the second argument. Also, you should pass a pointer of type void * as the third argument, not an integer. A function name evaluates to a pointer to the function, so you can simply pass the function name as the argument value, or you can use the address-of operator & before the function name.

int arg = 4;

// both calls are equivalent

rt_task_start(&demo_task1, demo, &arg);
rt_task_start(&demo_task1, &demo, &arg);
ajay
  • 8,550
  • 7
  • 34
  • 67
0

I'm not sure how the code in (1) can possibly compile. But here is what you should be using:

int rt_task_start (RT_TASK *task, void(*task_func)(void *arg), void *arg);
int val = 1;
rt_task_start(&demo_task1, demo, &val);

You cannot pass the function pointer bound to a specific argument, that is something like a closure, which isn't available in C. You can, however, pass the function pointer and then separately pass the argument you want to apply (which is what the function signature suggests you should do). But you must pass that argument as a pointer, not a literal.

Jeremy West
  • 8,287
  • 1
  • 15
  • 25
0

Surely you want:

  int i=1;
  rt_task_start(&demo_task1, demo, (void*) &i);

Just by matching the argument types, remember the second argument is just a function pointer, not a function call with its own argument, it's own argument is only used when you call it within rt_task_demo. If you then want to use the value '1' in function 'rt_task_demo' you would recast it like

int ii = *(int*) arg;
user3353819
  • 785
  • 2
  • 7
  • 18