Fix “dereferencing pointer to incomplete type” in C
C permits pointers like `struct Node *` to be declared when only a forward declaration (`struct Node;`) is visible, because all struct pointers have known size. However, dereferencing `ptr->member` requires the full member layout.
Quick Fix Summary
Move or #include the full struct Name { ... }; definition above any function accessing ptr->member, or add accessor functions when keeping a type intentionally opaque.
Why This Error Happens
Struct pointers vs member offsets
Computing node->next needs the exact byte offset of next inside struct Node. Without seeing every field declaration, the compiler cannot generate the memory load.
Missing standard headers (e.g. <time.h> or <stdio.h>)
Using internals of opaque library structs or struct tm * without #include <time.h> produces this same incomplete-type error.
Top Root Causes & Specific Code Fixes
Forward-declared struct never defined before field access
Writing struct User; lets you accept struct User *u, not read u->id.
struct User;
int get_id(struct User *u) { return u->id; }struct User { int id; };
int get_id(struct User *u) { return u->id; }Self-referential struct typedef typo (Node vs struct Node)
Inside struct Node { Node *next; }, typedef Node does not exist until after the closing brace.
typedef struct {
int val;
Node *next;
} Node;typedef struct Node {
int val;
struct Node *next;
} Node;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 Point;
void show(struct Point *p) {
printf("%d\n", p->x);
}
struct Point { int x; };
int main(void) { struct Point pt = {7}; show(&pt); return 0; }Open broken program in compilerRead the corrected program
#include <stdio.h>
struct Point { int x; };
void show(const struct Point *p) {
printf("%d\n", p->x);
}
int main(void) { struct Point pt = {7}; show(&pt); return 0; }Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
7Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| invalid use of undefined type `struct S` | Struct only forward-declared before `p->field` | -Wall -H | Include full `struct S { ... };` definition first |
| unknown type `Node` inside struct body | Self-pointer used typedef alias too early | -Wpedantic | Write `struct Node *next;` inside `typedef struct Node` |
Debugging Techniques and Commands
gcc -std=c17 -Wall -Wextra -H main.c -o app
Lists header inclusion order to reveal missing or cyclic struct headers.
gcc -std=c17 -Wpedantic -Wall main.c -o app
Validates ISO C tag and typedef declarations strictly.
Frequently Asked Questions
Why is typedef struct Node { struct Node *next; } Node; required?
Because the typedef name `Node` is only bound after the closing brace; inside the struct body only the tag `struct Node` is in scope.
When should I intentionally keep a struct incomplete?
Opaque pointer APIs deliberately put `struct Context;` in public `.h` headers so external callers pass pointers without depending on private fields.
Can sizeof work on an incomplete struct?
No. `sizeof(struct Foo)` requires the complete definition for the exact byte size.