Equivalent C declarations

前端 未结 5 1488
南笙
南笙 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:03

    I tend to follow The Precedence Rule for Understanding C Declarations which is given very nicely in the book Expert C Programming - Deep C Secrets by Peter van der Linden

    A - Declarations are read by starting with the name and then reading in 
    precedence order.
    
    B - The precedence, from high to low, is:
            B.1 parentheses grouping together parts of a declaration
            B.2 the postfix operators:
            parentheses () indicating a function, and
            square brackets [] indicating an array.
            B.3 the prefix operator: the asterisk denoting "pointer to".
    
    C If a const and/or volatile keyword is next to a type specifier (e.g. int, 
            long, etc.) it applies to the type specifier. 
            Otherwise the const and/or volatile keyword 
            applies to the pointer asterisk on its immediate left.
    

提交回复
热议问题