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. C++ onlyThe z/OS® XL C/C++ compiler supports this feature as an IBM extension. C++ only

Read syntax diagramSkip visual syntax diagram
Variable length array declarator syntax

>>-array_identifier--[--+-expression-------------+--]----------><
                        '-+-----------------+--*-'      
                          '-type-qualifiers-'           

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.

Note: C++ only In C++ applications, storage allocated for use by variable length arrays is not released until the function they reside in completes execution. C++ only

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 statement. 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 extensionC++ onlyThe C++ extension does not include support for references to a variable length array type; neither might a function parameter be a reference to a variable length array type. C++ onlyIBM extension