Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are qualifiers within the array declaration in function parameter lists an optional or mandatory feature in C17

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading