C isn't that hard: void ( *( *f[] ) () ) ()

前端 未结 13 819
名媛妹妹
名媛妹妹 2020-11-29 14:25

I just saw a picture today and think I\'d appreciate explanations. So here is the picture:

I found this confusing and wondered if such codes are ever prac

13条回答
  •  孤街浪徒
    2020-11-29 15:05

    In C, declaration mirrors usage—that’s how it’s defined in the standard. The declaration:

    void (*(*f[])())()
    

    Is an assertion that the expression (*(*f[i])())() produces a result of type void. Which means:

    • f must be an array, since you can index it:

      f[i]
      
    • The elements of f must be pointers, since you can dereference them:

      *f[i]
      
    • Those pointers must be pointers to functions taking no arguments, since you can call them:

      (*f[i])()
      
    • The results of those functions must also be pointers, since you can dereference them:

      *(*f[i])()
      
    • Those pointers must also be pointers to functions taking no arguments, since you can call them:

      (*(*f[i])())()
      
    • Those function pointers must return void

    The “spiral rule” is just a mnemonic that provides a different way of understanding the same thing.

提交回复
热议问题