Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deduce from the C++ Standard that an array [] has higher precedence than a pointer?

I know that a declaration like

int* a[5];

declares a as an array of 5 pointers to int. But how do I deduce this, just by using the C++ Standard?

like image 771
Ayrosa Avatar asked Apr 25 '21 20:04

Ayrosa


1 Answers

Just see the grammar of C++.

ptr-declarator:
    noptr-declarator
    ptr-operator ptr-declarator
noptr-declarator:
    declarator-id attribute-specifier-seqopt
    noptr-declarator parameters-and-qualifiers
    noptr-declarator [ constant-expressionopt] attribute-specifier-seqopt
    ( ptr-declarator )

and

ptr-operator:
    * attribute-specifier-seqopt cv-qualifier-seqopt
    & attribute-specifier-seqopt
    && attribute-specifier-seqopt
    nested-name-specifier * attribute-specifier-seqopt cv-qualifier-seqopt

Thus this declaration

int* a[5];

may be written like

int ( * ( ( a )[5] ) );

That is ( ( a )[5] ) is a noptr-declarator and ( * ( ( a )[5] ) ) is ptr-operator ptr-declarator.

A declaration of a pointer to an array of 5 integers will look like

int ( *a )[5];
like image 180
Vlad from Moscow Avatar answered Nov 15 '22 19:11

Vlad from Moscow