tags:

views:

35

answers:

2

hello everyone, 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

+1  A: 

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.

Pascal Cuoq
A: 

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