与此声明混淆 - int(*(*foo)(double))[3]

Way*_*ang 3 c++

我在这份声明中迷失了 - int (*(*foo)(double))[3];

我的理解是这是一个大小为3的数组,该元素是一个带有double的函数指针并返回指向int的指针.但是,正确的解释似乎是"指向函数采用double并返回指向3 int数组的指针".返回指针到3 INT的阵列混淆我很多作为int和[3]相距甚远.

这是为什么?声明像这个复杂的变量的语法或规则是什么?

CB *_*ley 7

你只是从里到外读出记住postfix array([])和函数"call"(())绑定比前缀pointer(*)更紧密:

      (*foo)              // foo is a pointer...
      (*foo)(double)      // to a function taking a double...
    (*(*foo)(double))     // returning a pointer...
    (*(*foo)(double))[3]  // to an array of 3...
int (*(*foo)(double))[3]; // ints
Run Code Online (Sandbox Code Playgroud)

(要找出从哪里开始你可能想从外面工作,但你需要从内到外阅读以传统的顺序阅读声明.)


P0W*_*P0W 5

您可以使用“顺时针螺旋规则”

      +----------------------------------+
      |  +---------------------------+   |
      |  |   +--------------------+  |   |
      |  |   | +----+             |  |   |
      |  |   | |+--+|             |  |   |
      |  |   | |^  ||             |  |   |
 int  (  *   ( *foo)( double      )  [3 ];
   |  ^  ^   ^ ^   ||             |  |   |
   |  |  |   | +---+|             |  |   |
   |  |  |   +------+             |  |   |
   |  |  +------------------------+  |   |
   |  +------------------------------+   |
   +-------------------------------------+  
Run Code Online (Sandbox Code Playgroud)

因此,标识符foo

  • 是一个指针
  • 以函数double作为参数返回
  • 指向
  • 数组3
  • 整数的

  • 请参阅[此处](http://stackoverflow.com/questions/16260417/the-spiral-rule-about-declarations-when-is-it-in-error)了解螺旋法则的优点。 (2认同)