我是C語言的初學者。閱讀函式指標上的各種 SO 執行緒。例如,函式指標的解參考是如何發生的,函式指標 - 自動解參考[重復],所以我嘗試做一個我的實驗。
無法理解為什么會引發此錯誤,因為我沒有在任何地方使用 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);
| ^~~~~~~~~~
uj5u.com熱心網友回復:
這個運算式陳述句
*recurse(2);
相當于
*( recurse(2) );
因此,由于函式的回傳型別是void你試圖取消參考 type void。
看來你的意思
( *recurse )(2);
或者你可以寫
recurse(2);
因為*recurse第一次呼叫中使用的運算式將再次隱式轉換為函式指標。
所以雖然這個電話例如
( ******recurse )(2);
是正確的,但是取消參考指標運算式是多余的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416419.html
標籤:
上一篇:在C中否定最大可能的負值
下一篇:使用c繪制圓、線、弧等
