Variable-Length Arrays

Variable length arrays are supported within local or function prototype scope, as required by the ISO/IEC 9899-1999 ("C99") standard. The following listing shows an example.

Listing: Example of C99 Variable Length Array usage

#pragma c99 on

void f(int n) {

   int arr[n];

   /* ... */

}

While the example shown in the following listing generates an error message.

Listing: 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 the following listing.

Listing: 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