分配函数指针正确的方法 [英] correct way to assign function pointer
问题描述
我有点困惑的正确语法分配函数指针
变量。如果我有一个函数foo
I'm a little confused about the correct syntax for assigning a function pointer to a variable. If I have a function foo
int foo();
和我分配一个指针到foo变量栏
and I am assigning a pointer to foo to variable bar
void * bar;
它似乎没有,如果我使用无关紧要
it does not seem to matter if I use
bar = foo;
// or
bar = &foo;
在我看来,只有其中之一应该是正确的还是我失去了一些东西?
It seems to me that only one of these should be correct or am I missing something?
推荐答案
富
和&放大器;富
值相当于C和具有相同的类型。
foo
and &foo
values are equivalent in C and have same type.
的&放大器;
这里运算符是正确的,但多余的
The &
operator here is correct but redundant.
请注意,分配函数指针无效*
是不是在C有效。
Note that assigning a function pointer to a void *
is not valid in C.
void *fp1 = foo; // invalid
int (*fp2)() = foo; // valid
int (*fp3)() = &foo; // valid
(这实际上是声明,但赋值运算符的限制适用。)
(These are actually declarations but the constraints of the assignment operator apply.)
这篇关于分配函数指针正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!