0

There is one thing in C that always seems weird for me, when using function pointers in function argument why sending the function name is the same as sending the function address?

void bar(void (*functionPtr)())
{
    doSomething
}

void foo(void)
{
    doSomething
}

int main()
{
    bar(&foo);
    bar(foo); // why is this the same? In C logic it's not supposed to work
    return (0);
}
Fayeure
  • 1,181
  • 9
  • 21
  • Would you expect the entire function to be sent, the machine bytes? The same reason you send the address of an array. Copying is wasteful and time consuming. – Fiddling Bits Mar 24 '21 at 19:57
  • C 2018 6.3.2.1 4 says “A *function designator* is an expression that has function type. Except when it is the operand of the `sizeof` operator, or the unary `&` operator, a function designator with type “function returning *type*” is converted to an expression that has type "pointer to function returning *type*”.” – Eric Postpischil Mar 24 '21 at 23:33

1 Answers1

0

Because the & is automatic. If you use the function name, it produces a pointer to the function. If you read up on the language spec, you'll see that the & is optional here.

JDługosz
  • 5,592
  • 3
  • 24
  • 45