2 questions about function pointer in c

前端 未结 1 423
滥情空心
滥情空心 2021-01-23 00:08

I have two questions

1.I saw that

int (*Ptr)(int,int);
Ptr=someOtherFuncion;

Its not should be like that?

Ptr=&som         


        
相关标签:
1条回答
  • 2021-01-23 00:25

    The name of a function decays almost immediately to a pointer to the function, so someOtherFunction decays to the same pointer that &someOtherFunction gives you explicitly. In fact, the operand of the address-of operator (&) is one of the few places were the decay doesn't happen.

    This has amusing consequences: Even if you dereference the function pointer, it decays again right away. So the following are all equivalent:

    someOtherFunction(1, 2);
    (*someOtherFunction)(1, 2);
    (**someOtherFunction)(1, 2);
    (***someOtherFunction)(1, 2);
    

    So, if you feel unwell assigning to a function pointer without an explicit address-of, by all means put the & in there, but you don't have to.

    To address the second part of the question: A function is always called through a function pointer, but because of the above-mentioned instant decay, normal functions can be called just the same way as function pointers.

    0 讨论(0)
提交回复
热议问题