Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are zero-length variable length arrays allowed/well defined?

Tags:

c

c99

I'm programming in C99 and use variable length arrays in one portion of my code. I know in C89 zero-length arrays are not allowed, but I'm unsure of C99 and variable length arrays.

In short, is the following well defined behavior?

int main()
{
    int i = 0;
    char array[i];
    return 0;
}
like image 454
Cornstalks Avatar asked Jul 09 '13 22:07

Cornstalks


1 Answers

No, zero-length arrays are explicitly prohibited by C language, even if they are created as VLA through a run-time size value (as in your code sample).

6.7.5.2 Array declarators

...

5 If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero.

like image 142
AnT Avatar answered Nov 16 '22 00:11

AnT