Operator Priority in C Definitions

Wikipedia claims that the operator [] precedes the operator * in the evaluation.

Then why the following statement:

 char *a[3]; 

declare an array of 3 character pointers, not a pointer to an array of 3 characters according to the priority of the operator?

+4
source share
3 answers

Because, as Wikipedia says, [] has a higher priority than * ?

Processing the declaration, a[3] treated as an "array of 3" before processing * .

To declare a pointer to a three-character array, you must use parentheses to override the default priority:

 char (*a)[3]; 

Brackets now take precedence over an array.

+7
source

Here the grammar for the declarator is taken from the standard (& section 6.7.5):

  declarator :
     pointer opt direct-declarator

 direct-declarator :
     identifier
     ( declarator )
     direct-declarator [ type-qualifier-list opt assignment-expression opt ]
     direct-declarator [ static type-qualifier-list opt assignment-expression ]
     direct-declarator [ type-qualifier-list static assignment-expression ]
     direct-declarator [ type-qualifier-list opt * ]
     direct-declarator ( parameter-type-list )
     direct-declarator ( identifier-list opt )

 pointer :
     * type-qualifier-list opt
     * type-qualifier-list opt pointer

 type-qualifier-list :
     type-qualifier
     type-qualifier-list type-qualifier

 parameter-type-list :
     parameter-list
     parameter-list , ...

 parameter-list :
     parameter-declaration
     parameter-list , parameter-declaration

 parameter-declaration :
     declaration-specifiers declarator
     declaration-specifiers abstract-declarator opt

 identifier-list :
     identifier
     identifier-list , identifier

As you can see, both [] and () bound to the declarator before * . Take an ad

 int *a[N]; 

The declaration *a[N] , which corresponds to the direct-declarator pointer of the opt pointer above, and thus is parsed as *(a[N]) , so a is an N-element array of the pointer.

Summarizing:

 T *a[N] -- declares an N-element array of pointer to T T (*a)[N] -- declares a pointer to an N-element array of T T *f() -- declares a function returning pointer to T T (*f)() -- declares a pointer to a function returning T 
+2
source

I got confused in the question - the interpretation of the declaration corresponds to the priority of the operator. If you want a pointer to an array, you must use parens to bind * to indentifier 'before binding [] .

 char (*a)[3]; 
0
source

All Articles