Pointer to function returning function pointer

前端 未结 2 1920
被撕碎了的回忆
被撕碎了的回忆 2021-02-07 04:37

I would like to declare a variable of type pointer to function returning pointer to function. Essentially what the following does, but without any typedefs

2条回答
  •  囚心锁ツ
    2021-02-07 05:25

    The general rule of C (and C++) declarations is: if you type the declaration as an expression, it will have the declaration's type.

    So, you want a pointer to function which returns pointer to function returning void.

    Let's say we have such a pointer, ptr. How to get void out of it?

    1. Dereference ptr, getting a function returning pointer to function returning void: *ptr

    2. Call the function, getting a pointer to function returning void: (*ptr)()

    3. Dereference that pointer, getting a function returning void: *(*ptr)()

    4. Call that function, getting void: (*(*ptr)())()

    Now just turn this into a declaration:

    void (*(*ptr)())();
    

    P.S. I know other have answered in the meantime (and I've upvoted). But I wanted to show the generic process to arrive at the declaration form.

提交回复
热议问题