Compilation (gcc error)Type & Declaration5 min read

Fix “storage size of struct isn't known” in C

Declaring a value variable (`struct Box b;`), stack array (`struct Box items[4];`), or calling `sizeof(struct Box)` forces the compiler to reserve exact stack bytes immediately. A forward `struct Box;` declaration is never enough for value allocation.

Quick Fix Summary

Provide or #include the full struct Type { ... }; definition before declaring variables or arrays of that type, or store a pointer struct Type * until the full definition is available.

Why This Error Happens

Pointers vs value storage

Only struct T *ptr has a fixed target-pointer width regardless of fields inside struct T. Declaring struct T value or embedding struct T inner; inside another struct requires the complete layout.

POSIX headers and feature test macros

POSIX structs such as struct timespec or struct sigaction under -std=c17 require their documented header (<time.h>, <signal.h>) plus _POSIX_C_SOURCE where applicable.

Top Root Causes & Specific Code Fixes

Allocating a value variable of a forward-declared struct

Writing struct Foo x; fails if only struct Foo; appeared earlier.

Broken Pattern
struct Config;
struct Config cfg;
Corrected Pattern
struct Config { int port; };
struct Config cfg = { 8080 };
Illustrative diagnostic (not executed):error: storage size of 'cfg' isn't known

Uninitialized extern/static array with missing dimension

Writing static int table[]; omits both a size and an initializer list.

Broken Pattern
static int primes[];
Corrected Pattern
static int primes[] = {2, 3, 5, 7};

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>
struct Pair;
int main(void) {
    struct Pair p;
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
struct Pair { int a; int b; };
int main(void) {
    struct Pair p = {10, 20};
    printf("%d\n", p.a + p.b);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

30

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
storage size of `x` isn't knownValue variable uses forward-declared struct-Wall -WextraDefine full struct body or store pointer `struct S *`
missing POSIX struct under `-std=c17`POSIX feature macro missing for `struct sigaction`-D_POSIX_C_SOURCE=200809LAdd POSIX feature macro or required system header

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

Compiler Diagnostics
gcc -std=c17 -Wall -Wextra main.c -o app

Points directly to the variable whose struct definition is missing or hidden behind a feature macro.

Size Check Verification
gcc -std=c17 -c main.c

Confirms every header provides complete type declarations before linking.

Frequently Asked Questions

Why does int main(void) fail with storage size isn't known on struct sigaction?

When compiling with strict `-std=c17`, POSIX extensions like `sigaction` are hidden unless `_POSIX_C_SOURCE` is defined above `<signal.h>` (or `-std=gnu17` is used).

Can two structs contain each other by value?

No. At least one struct must hold a pointer (`struct B *`) to break infinite recursion.

Why does empty static int arr[]; fail?

Internal-linkage arrays need either an explicit element count (`[4]`) or a brace initializer so the compiler knows how many bytes to reserve.