为什么只要函数指针的行为类似于函数本身,它们就会存在?
我知道有一个类似的话题,但这些答案并没有说明我想知道什么。因此,只要从下面的摘录中可以注意到一个函数和对该函数的引用的行为方式相同,那么同时拥有函数变量和函数指针又有什么意义呢?
#include <stdio.h>
int f(){ return 0;}
int main() {
printf("%pn",f);
printf("%pn",&f);
printf("%pn",f + 1);
printf("%pn",&f + 1);
return 0;
}
此外, f 和 &f 都可以作为其他函数的参数传递。
回答
f并且&f意思相同,所以没有必要使用&f.
但是语言设计者出于某种原因决定不让它无效,所以你有这个多余的语法。
- Look at the `qsort()` function. One of the arguments is a pointer to the comparison function, since this can be different for different arrays.