![](/img/trans.png)
[英]warning: dereferencing ‘void *’ pointer [enabled by default] and error: void value not ignored as it ought to be help c
[英]dereferencing function pointer to function returning void throws error: void value not ignored as it ought to be
我是 C 语言的初学者。 读取 function 指针上的各种 SO 线程。 例如, function 指针的解引用是如何发生的, Function 指针 - 自动解引用 [重复] ,所以我试着做一个我的实验。
无法理解为什么会引发此错误,因为我没有在任何地方使用 void 值..
#include <stdio.h>
void f(int j) {
static int i;
if (j == 1)
printf("f: i entered this function main()\n");
else if(j == 2)
printf("f: i entered this function through pointer to function\n");
void (*recurse)(int);
recurse = *f; // "f" is a reference; implicitly convert to ptr.
if (i == 0){
i++;
*recurse(2);
} else
return;
}
int main(void)
{
f(1);
return 0;
}
GCC 版本是 11.2.0 使用的编译器标志包括 -std=c99 如果我将第 14 行修改为recurse(2)
,那么程序运行顺利。 编译器不会抛出任何错误或警告。
$ gcc testing11.c @compilerflags11.2.0.txt -o testing11.exe
testing11.c: In function ‘f’:
testing11.c:14:10: error: void value not ignored as it ought to be
14 | *recurse(2);
| ^~~~~~~~~~
这个表达式语句
*recurse(2);
相当于
*( recurse(2) );
因此,由于 function 的返回类型是void
,那么您正在尝试取消引用类型void
。
看来你的意思
( *recurse )(2);
或者你可以写
recurse(2);
因为第一次调用中使用的表达式*recurse
将再次隐式转换为 function 指针。
所以虽然这个电话例如
( ******recurse )(2);
是正确的,但是取消引用指针表达式是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.