简体   繁体   中英

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM