Compilation (gcc error)Type & Declaration5 min read

Fix “variable-sized object may not be initialized” in C

In C99, C11, and C17, an array whose bound is not an integer constant expression is a variable-length array (VLA), and ISO C17 forbids attaching an initializer list like `= {0}` to a VLA. Note that even `const int n = 5;` is NOT an integer constant expression in C (unlike C++), so `int arr[n] = {0};` is treated as a VLA and rejected prior to C23.

Quick Fix Summary

Use #define N 5 or enum { N = 5 }; when the bound is fixed at compile time; when n comes from runtime input, validate n > 0 within safe bounds and use calloc((size_t)n, sizeof *arr) (with free) or memset(arr, 0, sizeof arr) on a bounded VLA.

Why This Error Happens

Surprising C rule: const int n = 10 still makes int a[n] a VLA

In C17, a const-qualified variable is read-only storage, not a compile-time constant expression. Use enum { N = 10 }; or #define N 10 for fixed-size arrays that support = {0}.

Guarding stack size when n comes from user input

Never allocate an unchecked int arr[n] from untrusted scanf input, because large n overflows the stack immediately—prefer calloc on the heap for dynamic sizes.

Top Root Causes & Specific Code Fixes

Writing int arr[n] = {0}; with a runtime variable n in C17

C99–C17 forbid brace initializers on variable-length arrays.

Broken Pattern
int n = 4;
int arr[n] = {0};
Corrected Pattern
int n = 4;
int arr[n];
memset(arr, 0, sizeof arr);
Illustrative diagnostic (not executed):error: variable-sized object may not be initialized

Assuming const int MAX = 8 creates a fixed-length array in C

Unlike C++, `const int` is not an integer constant expression in C17.

Broken Pattern
const int MAX = 8;
int counts[MAX] = {0};
Corrected Pattern
enum { MAX = 8 };
int counts[MAX] = {0};

Complete Broken and Corrected Programs

Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.

Read the broken program
#include <stdio.h>
int main(void) {
    int n = 4;
    int arr[n] = {0};
    printf("%d\n", arr[0]);
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
    size_t n = 4;
    int *arr = calloc(n, sizeof *arr);
    if (!arr) return 1;
    printf("%d\n", arr[0]);
    free(arr);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

0

Debugging Techniques and Commands

Detect Accidental VLAs (-Wvla)
gcc -std=c17 -Wall -Wextra -Wvla main.c -o app

Warns whenever an array declaration silently creates a runtime stack VLA.

Heap Zero-Initialization
int *arr = calloc(count, sizeof *arr);

Safely allocates and zero-initializes runtime-sized arrays with NULL failure checking.

Frequently Asked Questions

Did C23 allow empty = {} initialization for VLAs?

Yes, ISO C23 permits empty brace zero-initialization `= {}` for VLAs, while non-empty initializer lists remain disallowed. Under `-std=c17` or `-std=c11`, use `memset` or `calloc`.

Does sizeof arr work on a VLA before passing it to a function?

In the same scope where `int arr[n]` was declared, `sizeof arr` evaluates at runtime to `n * sizeof(int)`. Once passed to a function parameter, it decays to a pointer.

Why is calloc safer than a VLA for large n?

`calloc` allocates on the heap, checks for `size * count` multiplication overflow, zeroes memory, and returns `NULL` on exhaustion instead of crashing the call stack.