Equivalent C declarations

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

Are

int (*x)[10];

and

int x[10];

equivalent?

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

5条回答
  •  梦毁少年i
    2021-02-04 21:16

    Follow this simple process when reading declarations:

    Start at the variable name (or innermost construct if no identifier is present. Look right without jumping over a right parenthesis; say what you see. Look left again without jumping over a parenthesis; say what you see. Jump out a level of parentheses if any. Look right; say what you see. Look left; say what you see. Continue in this manner until you say the variable type or return type.

    So:

    int (*x)[10];
    

    x is a pointer to an array of 10 ints

    int x[10];
    

    x is an array of 10 ints

    int *x[10];
    

    x is an array of 10 pointers to ints

提交回复
热议问题