Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Variable-length array

A VLA gets its size when its declaration executes but does not subsequently resize. A pointer to a VLA illustrates the broader category of VM types.

Version: C23; repository compiler flag, production runtime not attested

Syntax

int a[n];
int (*p)[n] = &a;

A variable length array (VLA), such as int a[n]; with variable n, does not require an integer constant expression for its size. An evaluated bound must be positive; each instance keeps its length.

A variably modified type (VM) includes VLAs and derived types. int (*p)[n] declares a pointer to a VLA, not an array. VM identifiers require block or function prototype scope and no linkage.

In C23, automatic VLAs are optional: __STDC_NO_VLA__ == 1 signals missing support; VM types remain required. Validate bounds before declaration and limit memory consumption. Automatic a lives from its declaration until execution leaves its scope.

Reference identifiers covered by this entry: variable-length array.

Examples

#include <stdio.h>

#if defined(__STDC_NO_VLA__) && __STDC_NO_VLA__ == 1
#error VLA support required
#endif

int main(void)
{
    int n = 3;
    int a[n];
    int (*p)[n] = &a;
    int sum = 0;

    for (int i = 0; i < n; ++i) {
        (*p)[i] = i + 1;
        sum += (*p)[i];
    }
    printf("%d\n", sum);
    return 0;
}

Result

6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources