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.
struct Config;
struct Config cfg;struct Config { int port; };
struct Config cfg = { 8080 };Uninitialized extern/static array with missing dimension
Writing static int table[]; omits both a size and an initializer list.
static int primes[];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 compilerRead 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 compilerExpected output from the corrected program
Reference output, not a live execution result.
30Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| storage size of `x` isn't known | Value variable uses forward-declared struct | -Wall -Wextra | Define 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=200809L | Add POSIX feature macro or required system header |
Debugging Techniques and Commands
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.
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.