Typedeffing a function (NOT a function pointer)

前端 未结 6 2244
醉酒成梦
醉酒成梦 2020-12-17 05:20
typedef void int_void(int);

int_void is a function taking an integer and returning nothing.

My question is: can it be

6条回答
  •  佛祖请我去吃肉
    2020-12-17 06:00

    This should work, no casting required:

    void f(int x) { printf("%d\n", x); }
    
    int main(int argc, const char ** argv)
    {
        typedef void (*int_void)(int);
        int_void test = f;
        ...
     }
    

    A function's name "devolves" into a function pointer anytime you use the function's name in something other than a function call. If is is being assigned to a func ptr of the same type, you don't need a cast.

    The original

    typedef int_void(int);
    

    is not useful by itself, without using a pointer to the type. So the answer to your question is "no, you can't use that typedef without a pointer".

提交回复
热议问题