这是我的 C 程序:
#include <stdio.h>
char const* voice(void){
return "hey!";
}
int main(){
const char* (*pointer)(void);
pointer = &voice;
printf ("%s\n", *pointer); // check down *
return 0;
}
- *我正在尝试打印从指针返回的内容,但似乎无法正常工作。
我究竟做错了什么?
这是我的 C 程序:
#include <stdio.h>
char const* voice(void){
return "hey!";
}
int main(){
const char* (*pointer)(void);
pointer = &voice;
printf ("%s\n", *pointer); // check down *
return 0;
}
我究竟做错了什么?
您需要调用函数指针,即使用括号:
#include <stdio.h>
char const* voice(void){
return "hey!";
}
int main(){
const char* (*pointer)(void);
pointer = &voice;
printf ("%s\n", pointer());
// ^^^^^^^^^
return 0;
}
*
函数指针不需要。(也不是&
)
您通过指针调用函数的方式与直接调用函数的方式完全相同,就好像该指针是函数的名称一样:
printf ("%s\n", pointer());
从 ANSI C 标准开始,函数指针周围的星号和 & 号是可选的。