C语言 错误无效使用 void 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22732007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:56:20  来源:igfitidea点击:

Error Invalid use of void expression

cxenomai

提问by Arun Gupta

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.

我有一个函数int rt_task_start (RT_TASK *task, void(*task_func)(void *arg), void *arg),在第二个参数中我传递了一个带参数的函数。

  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 castthen 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.
  1. 当我当时只传递一个函数名时,没有问题。(正如预期的那样工作)。 rt_task_start(&demo_task1, demo, 1);
  2. 但是当我通过时,rt_task_start(&demo_task1, demo(&val), 1);它给了我错误error: invalid use of void expression。变量 val 是之前定义的。int val = 0;
  3. 当我与此事致电 rt_task_start(&demo_task1, demo(val), 1);这是示值误差 Warning passing argument 1 of 'demo' makes pointer from integer without a cast然后error: invalid use of void expression
  4. int *val; *val = 0;rt_task_start(&demo_task1, demo(&val), 1);这也给了我错误。

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

我不明白我应该传递什么,作为一个空指针。它给了我错误。任何想法请!

采纳答案by ajay

void (*task_func)(void *arg);

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

上面的语句定义task_func为一个指向函数的指针,该函数接受一个类型的指针void *并且不返回任何值。

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.

因此,当您调用 function 时rt_task_start,您应该将指向函数的指针作为第二个参数传递。此外,您应该将类​​型指针void *作为第三个参数传递,而不是整数。函数名称计算为指向函数的指针,因此您可以简单地将函数名称作为参数值传递,或者您可以&在函数名称之前使用地址运算符。

int arg = 4;

// both calls are equivalent

rt_task_start(&demo_task1, demo, &arg);
rt_task_start(&demo_task1, &demo, &arg);

回答by user3353819

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

只需匹配参数类型,记住第二个参数只是一个函数指针,而不是带有自己参数的函数调用,它自己的参数仅在您在 rt_task_demo 中调用时使用。如果你想在函数“rt_task_demo”中使用值“1”,你可以像

int ii = *(int*) arg;

回答by Jeremy West

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

我不确定 (1) 中的代码如何编译。但这是您应该使用的:

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.

您不能传递绑定到特定参数的函数指针,这类似于闭包,在 C 中不可用。但是,您可以传递函数指针,然后单独传递要应用的参数(这就是函数签名建议您应该这样做)。但是您必须将该参数作为指针传递,而不是文字。