0

In C++, when you declare pointer, normaly the syntax is

datatype* ptr;

But when you declare function pointer, you write

datatype (*ptr) (param1, param2);

datatype* means pointer to datatype, *ptr means a content pointer points to.

So it's confusing why it is datatype (*ptr), not datatype* ptrwhen you declare function pointer.

Thank you.

Thomas Jager
  • 4,836
  • 2
  • 16
  • 30
  • 2
    `datatype *(ptr) (param1, param2);` Would be a function returning a pointer. – Thomas Jager Jun 20 '20 at 15:05
  • C and C++ are strictly typed languages. The compiler needs to be able to differentiate between a pointer to a data type versus a pointer to a function, and it needs to know the full signature of the type of function being pointed at so it can actually call functions via such a pointer. – Remy Lebeau Jun 20 '20 at 19:08
  • `datatype (*ptr);` is a perfectly valid declaration and is equivalent to `datatype* ptr;`; it just has redundant parentheses. In the case of the function pointer, the parentheses are required to override the higher precedence of the function arguments -- postfix operators are always higher precedence than prefix in C++. – Chris Dodd Jun 20 '20 at 19:19

0 Answers0