2

I am writing a custom C99 parser. I got the grammar from this link. This grammar says following is a valid syntax for declaring arrays -

int arr[*];

Relevant part of the grammar is follwing -

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

I tried compiling a code with this declaration using gcc. It gave me following warning -

error: ‘[*]’ not allowed in other than function prototype scope

So I tried declaring a function prototype with this type of syntax and it compiled without any error or warning. What I am not getting is what can this syntax possibly mean semantically. Any expert with an explanation?

taufique
  • 2,701
  • 1
  • 26
  • 40
  • It is just an explicit way to express that no value is being passed for that array dimension. (you must always provide the last, but preceding dimensions are optional) (that's the link I was looking for) – David C. Rankin Feb 19 '17 at 10:13
  • maybe read the rest of the standard, if you're going to write a parser? – M.M Feb 19 '17 at 11:32

1 Answers1

3

It's a declarator for a variable length array with unspecified size. Furhtermore, the following declaration

void func(size_t n, char s[n]);

is equivalent to simply writing:

void func(size_t n, char s[*]);

The above is particularly useful for writing headers, where you'd normally declare only the parameter types

void func(size_t, char [*]);
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458