Swift Swift - 1 year ago 100
C Question

Why there are two ways to call function by pointer?

In project I'm working with I had found a complex macro that was creating an map of pointers to functions loaded from shared library. To use it for another library which should be loaded same way I had to inspect that.. and found that it contains line that shouldn't be working as far as I know. To boil down it to simple code, I threw away all syntax sugar and made such example:

#include <iostream>

using namespace std;

void func(int i, float f)
cout << "Parameter is " << i << " and " << f << endl;

typedef void (*FunctionFunc)(int i, float f);

int _tmain(int argc, _TCHAR* argv[])
FunctionFunc p = &func;
return 0;

Shouldn't the function call look like (*p)(5,4.3f) - according to books since K&R's? Compiler is VS2010 and code above works both with asterisk and without.

Answer Source

No, the dereference is not necessary. Pointers to functions are callable just like functions are. Even p(5, 4.3f) would have been acceptable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download