Variable-Length Arrays

Variable length arrays are supported within local or function prototype scope, as required by the ISO/IEC 9899-1999 ("C99") standard. Example of C99 Variable Length Array usage shows an example.

Listing 1. Example of C99 Variable Length Array usage
#pragma c99 on


void f(int n) {
   int arr[n];
   /* ... */

}

While the example shown in Bad Example of C99 Variable Length Array usage generates an error message.

Listing 2. Bad Example of C99 Variable Length Array usage
#pragma c99 on
int n;
int arr[n];
// ERROR: variable length array
// types can only be used in local or 

// function prototype scope. 

A variable length array cannot be used in a function template's prototype scope or in a local template typedef , as shown in Bad Example of C99 usage in Function Prototype.

Listing 3. Bad Example of C99 usage in Function Prototype
#pragma c99 on


template<typename T> int f(int n, int A[n][n]);
{
};
// ERROR: variable length arrays
// cannot be used in function template prototypes

// or local template variables