Variable length arrays

A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.

Read syntax diagramSkip visual syntax diagramVariable length array declarator syntax

If the size of the array is indicated by * instead of an expression, the variable length array is considered to be of unspecified size. Such arrays are considered complete types, but can only be used in declarations of function prototype scope.

A variable length array and a pointer to a variable length array are considered variably modified types. Declarations of variably modified types must be at either block scope or function prototype scope. Array objects declared with the extern storage class specifier cannot be of variable length array type. Array objects declared with the static storage class specifier can be a pointer to a variable length array, but not an actual variable length array. The identifiers declared with a variably modified type must be ordinary identifiers and therefore cannot be members of structures or unions. A variable length array cannot be initialized.

A variable length array can be the operand of a sizeof expression. In this case, the operand is evaluated at run time, and the size is neither an integer constant nor a constant expression, even though the size of each instance of a variable array does not change during its lifetime.

A variable length array can be used in a typedef expression. The typedef name will have only block scope. The length of the array is fixed when the typedef name is defined, not each time it is used.

A function parameter can be a variable length array. The necessary size expressions must be provided in the function definition. The compiler evaluates the size expression of a variably modified parameter on entry to the function. For a function declared with a variable length array as a parameter, as in the following,

void f(int x, int a[][x]);

the size of the variable length array argument must match that of the function definition.

IBM extension C++ The C++ extension does not include support for references to a variable length array type; neither may a function parameter be a reference to a variable length array type.

Related information