Home > database >  Are qualifiers within the array declaration in function parameter lists an optional or mandatory fea
Are qualifiers within the array declaration in function parameter lists an optional or mandatory fea

Time:08-24

Quoting the cppreference site

In function parameter lists, additional syntax elements are allowed within the array declarators: the keyword static and qualifiers, which may appear in any order before the size expression (they may also appear even when the size expression is omitted).

and

If qualifiers are present, they qualify the pointer type to which the array parameter type is transformed:

int f(const int a[20])
{
// in this function, a has type const int* (pointer to const int)
}
int g(const int a[const 20])
{
// in this function, a has type const int* const (const pointer to const int)
}

However the snippet above won't compile on MSVC v19 using the /std:c17 flag, failing wth the error:

<source>(5): error C2143: syntax error: missing ']' before 'const'
<source>(5): error C2143: syntax error: missing ')' before 'const'
<source>(5): error C2143: syntax error: missing '{' before 'const'
<source>(5): error C2059: syntax error: 'constant'
<source>(5): error C2059: syntax error: ')'

https://godbolt.org/z/8r9388PYn

Is this a MSVC bug or is this specification optional?

If this is a mandatory specification can you redirect me to the relative paragraph of the C17 standard?

CodePudding user response:

The C standard requires a conforming C implementation to accept these qualifiers (C 2018 6.7.6 1 and 6.7.6.3 7). MSVC is not a conforming compiler.

6.7.6 1 shows the grammar for declarators, which includes:

  • direct-declarator [ type-qualifier-listopt assignment-expressionopt ]

There is no indication that this part of the grammar is optional.

6.7.6.3 7 specifies the interpretation of the qualifiers:

A declaration of a parameter as “array of type” shall be adjusted to “qualified pointer to type”, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation…

  •  Tags:  
  • c
  • Related