Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?

前端 未结 3 655
失恋的感觉
失恋的感觉 2020-11-21 23:21

Why do the following work?

void foo() {
    cout << \"Foo to you too!\\n\";
};

int main() {
    void (*p1_foo)() = foo;
    void (*p2_foo)() = *foo;
          


        
3条回答
  •  礼貌的吻别
    2020-11-22 00:23

    I think it's also helpful to remember that C is just an abstraction for the underlying machine and this is one of the places where that abstraction is leaking.

    From the perspective of the computer, a function is just a memory address which, if executed, performs other instructions. So a function in C is itself modelled as an address, which probably leads to the design that a function is "the same" as the address it points to.

提交回复
热议问题