0

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;
    (p)(5,4.3f);
    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.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • 1
    You might want to have a look at [this](http://stackoverflow.com/questions/12152167/why-is-using-the-function-name-as-a-function-pointer-equivalent-to-applying-the) – nishantsingh Sep 21 '16 at 10:33
  • @user3286661: That's the reverse conversion. – MSalters Sep 21 '16 at 10:34
  • @MSalters The accepted answer to that question is a very good answer to this question. – Klas Lindbäck Sep 21 '16 at 10:48
  • Well, it was not possible to find that because that question was an answer to my question, lol. The author asked about quite different manifestation of same paradox. I can't say I blame newer standards or that I hate C++, but by piling up features into standard they caused some issues. – Swift - Friday Pie Sep 21 '16 at 11:47
  • Fun fact: the function-call operator, `()`, *always* operates on a pointer-to-function. When you write `(*p)()`, the pointer is dereferenced, _and then immediately un-dereferenced again_ so that the function call operator gets what it wants. (C99 §§ 6.3.2p4 and 6.5.2.2p1) – zwol Sep 21 '16 at 12:47

1 Answers1

1

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

MSalters
  • 173,980
  • 10
  • 155
  • 350