In C, what is the difference between `&function` and `function` when passed as arguments?

后端 未结 4 1696
醉酒成梦
醉酒成梦 2020-12-05 07:41

For example:

#include 

typedef void (* proto_1)();
typedef void proto_2();

void my_function(int j){
    printf(\"hello from function. I got          


        
4条回答
  •  暖寄归人
    2020-12-05 08:36

    there is no difference between &function and function when passing as arguement

    however there is a difference between your typedefs. I do not know the official explanation, i.e what exactly the difference, but from what i remember

    typedef void (*name1)(void);
    

    and

    typedef void(name2)(void);
    

    are different:

    name1 is a pointer to a function that takes no paramter and returns nothing

    name2 is a function that takes no paramter and returns nothing

    you can test it by compiling:

    typedef void (*pointer)(void);
    typedef void (function)(void);
    
    void foo(void){}
    
    int main()
    {
        pointer p;
        function f;
    
        p = foo; //compiles
        p();
    
        f = foo; //does not compile
        f();
    }
    

    again, i am not the right person to explain exact reason of this behavior but i believe if you take a look at standards you will find the explanation somewhere there

提交回复
热议问题