Equivalent C declarations

前端 未结 5 1487
南笙
南笙 2021-02-04 20:18

Are

int (*x)[10];

and

int x[10];

equivalent?

According to the \"Clockwise Spiral\" rule, they parse

5条回答
  •  借酒劲吻你
    2021-02-04 21:00

    For me, it's easier to remember the rule as absent any explicit grouping, () and [] bind before *. Thus, for a declaration like

    T *a[N];
    

    the [] bind before the *, so a is an N-element array of pointer. Breaking it down in steps:

       a     -- a
       a[N]  -- is an N-element array
      *a[N]  -- of pointer
    T *a[N]  -- to T.
    

    For a declaration like

    T (*a)[N];
    

    the parens force the * to bind before the [], so

        a      -- a
      (*a)     -- is a pointer
      (*a)[N]  -- to an N-element array
    T (*a)[N]  -- of T
    

    It's still the clockwise/spiral rule, just expressed in a more compact manner.

提交回复
热议问题