问题描述
在 C 中有一件事情对我来说总是很奇怪,当在函数参数中使用函数指针时,为什么发送函数名称与发送函数地址相同?
void bar(void (*functionPtr)())
{
doSomething
}
void foo(void)
{
doSomething
}
int main()
{
bar(&foo);
bar(foo); // why is this the same? In C logic it's not supposed to work
return (0);
}
解决方法
因为 &
是自动的。如果使用函数名,它会生成一个指向该函数的指针。如果您阅读语言规范,您会发现 &
在这里是可选的。