0

If I have the following code example,

...

typedef void (*printer_t)(int);

int main()
{
    printer_t p = &print_int;
    p(5);
    return 0;
}

Why does both p(5); and (*p)(5); output the desired behaviour of printing 5? If I have a pointer to a function, is that not first required to be dereferenced and then be called, i.e. (*p)(5); was the correct behaviour? How does p(5); not result in an error by trying to use a calling operation on a memory address location?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
J.Doe
  • 31
  • 4
  • Pointers to functions are unusual. You could get the address of `print_int` with just plain `print_int`, with `&print_int`, with `&&print_int`, etc. And you can call through that pointer with `p(5)`, with `(*p)(5)`, `(**p)(5)`, `(***p)(5)`, etc. Everything collapses. – Pete Becker Dec 16 '20 at 01:10

1 Answers1

0

Why can a function pointer be called without deferencing it?

Because the standard says that you can.

is that not first required to be dereferenced

There is no need to explicitly use the indirection operator, no.

eerorika
  • 232,697
  • 12
  • 197
  • 326