在 C 中使用解引用运算符 (*) 指向函数的指针
Pointer to function with dereferencing operator(*) in C
我对指向函数的指针有点困惑。
假设
int func (int parameter){
return parameter + 5;
}
int (* pfunction)(int) = func;
pfunction(5); //this will return 10
但我想知道没有括号是什么意思。像这样。
pfunction
*pfunction
我知道在指向 int、float、double 的指针的情况下这两者之间的区别......但是我不知道这两个在涉及 pointer to 时是什么意思函数.
你能解释一下吗?
pfunction
表示函数指针,它保存着函数的引用
*pfunction
取消引用的函数指针将对函数提供相同的引用
&pfunction
pfunction
指针的地址
当调用函数指针时,两种形式都是正确的:pfunction(5) and (*pfunction)(5)
示例:
int func (int parameter){
return parameter + 5;
}
int main(void)
{
int (*pfunction)(int) = func;
printf("%d\n", pfunction(5)); //this will return 10
printf("%d\n", (*pfunction)(5)); //this will return 10
printf("pfunction \t%p\n", (void *)pfunction);
printf("*pfunction \t%p\n", (void *)*pfunction);
printf("&pfunction \t%p\n", (void *)&pfunction);
printf("%p\n", (void *)func);
printf("%p\n", (void *)main);
}
我对指向函数的指针有点困惑。
假设
int func (int parameter){
return parameter + 5;
}
int (* pfunction)(int) = func;
pfunction(5); //this will return 10
但我想知道没有括号是什么意思。像这样。
pfunction
*pfunction
我知道在指向 int、float、double 的指针的情况下这两者之间的区别......但是我不知道这两个在涉及 pointer to 时是什么意思函数.
你能解释一下吗?
pfunction
表示函数指针,它保存着函数的引用
*pfunction
取消引用的函数指针将对函数提供相同的引用
&pfunction
pfunction
指针的地址
当调用函数指针时,两种形式都是正确的:pfunction(5) and (*pfunction)(5)
示例:
int func (int parameter){
return parameter + 5;
}
int main(void)
{
int (*pfunction)(int) = func;
printf("%d\n", pfunction(5)); //this will return 10
printf("%d\n", (*pfunction)(5)); //this will return 10
printf("pfunction \t%p\n", (void *)pfunction);
printf("*pfunction \t%p\n", (void *)*pfunction);
printf("&pfunction \t%p\n", (void *)&pfunction);
printf("%p\n", (void *)func);
printf("%p\n", (void *)main);
}