开发者

declaration of the pointer to the function

let's assume I have this snippet of the code:

void foo_for_foo( void some_function(int, int))     <------
{
    int x = 5;
    some_function(x, x);
}

and also this one (actually the same with small difference)

void foo_for_foo( void (*some_function)(int, int))  <-------
{
    int x = 5;
    some_function(x, x);
}

my question is, does it matter how do I write it

void foo_for_foo( void some_function(int, int))

or

void foo开发者_如何学运维_for_foo( void (*some_function)(int, int))

cause in both cases I receive the same result thanks in advance


Yes, both versions mean the same thing: functions and function pointers are converted into each other (both ways). See also section 4 in this list of strange behaviors of standard C:

http://www.eecs.berkeley.edu/~necula/cil/cil016.html#toc32

Basically, what happens there is that pointers are converted to functions, or functions to pointers, multiple times in an unintuitive way.


Both versions are the same, since you're essentially passing a pointer to the function.

Though the code is correct, it is often better to use a typedef for the function pointer, which improves code readability and makes for more concise, elegant code.

Something like this:

typedef void (*FooFunction)(int,int);

void foo_for_foo (FooFunction fnc)
{
    int x = 5;
    fnc(x, x);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜