Describe: int (*(*var[3])())(void (*)());

天涯浪子 提交于 2019-12-14 04:12:13

问题


int (*(*var[3])())(void (*)());

How would you describe the type of var in the above declaration?

I get:

Declares var as array of 3 pointers to functions (A)

  • These functions (A) take any inputs and return a pointer to a function (B)

  • These functions (B) take a pointer to a function (C) and return int

  • These functions (C) take any inputs and return nothing

Is this right? Thanks


回答1:


Start with the leftmost identifier and work your way out, remembering that () and [] bind before *, so:

T *a[N]      // a is an array of pointer to T
T (*a)[N]    // a is a pointer to an array of T
T *f()       // f is a function returning pointer to T
T (*f)()     // f is a pointer to a function returning T

Edit

Although it doesn't show up in this declaration, const can introduce its own share of wrinkles:

T const *p   // p is a pointer to constant T
const T *p   // same as above

In both of these cases, p points to a constant T. You can write to p (make it point to a different object), but you cannot write to *p (you cannot update the value of the thing p points to).

T * const p  // p is a constant pointer to T

This declares p as a constant pointer to T; You can write to *p (update the value of the thing p points to, assuming *p results in a modifiable lvalue), but you cannot write to p (you can't make it point to a different object).

End edit

So

        var                      -- var is a
        var[3]                   -- 3-element array of
       *var[3]                   -- pointer to
      (*var[3])()                -- function taking 
      (*var[3])()                --   unspecified parameters 
     *(*var[3])()                -- returning pointer to
    (*(*var[3])())(          )   -- function taking
    (*(*var[3])())(          )   --   unnamed parameter is a
    (*(*var[3])())(      *   )   --   pointer to
    (*(*var[3])())(     (*)())   --   function taking
    (*(*var[3])())(     (*)())   --     unspecified parameters
    (*(*var[3])())(void (*)())   --   returning void
int (*(*var[3])())(void (*)());  -- returning int

So, var is a 3-element array of pointers to functions, each of which returns a pointer to another function (which takes a pointer to yet another function as an argument) that returns int.



来源:https://stackoverflow.com/questions/39348447/describe-int-var3void

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!