Why does `function == &function`?

57 Views Asked by At

I've always done callback = functionpointer, but found callback = &functionpointer in some code recently and thought it was strange, expecting that &functionpointer would effectively give a double-pointer.

So I tried this example:

#include <stdio.h>

void f(int i)
{
    printf("%d\n", i);
}

int main()
{
    void (*g)(int) = f;  // without &
    void (*h)(int) = &f; // _with_ &

    printf("g=%p\n", g);
    printf("h=%p\n", h);
    g(1);
    h(2);
}

which prints this:

g=0x401126
h=0x401126
1
2

I was surprised that it didn't crash, and equally surprised that f == &f (which is, of course, why it didn't crash).

What is the deal here? Is this just C spec, or some other nuanced "feature" (that I'd like to learn more about)?

0

There are 0 best solutions below