Compilation (gcc error)Type & Declaration6 min read

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.

Broken Pattern
struct User;
int get_id(struct User *u) { return u->id; }
Corrected Pattern
struct User { int id; };
int get_id(struct User *u) { return u->id; }
Illustrative diagnostic (not executed):error: invalid use of undefined type 'struct User'

Self-referential struct typedef typo (Node vs struct Node)

Inside struct Node { Node *next; }, typedef Node does not exist until after the closing brace.

Broken Pattern
typedef struct {
    int val;
    Node *next;
} Node;
Corrected Pattern
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 compiler
Read 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 compiler

Expected output from the corrected program

Reference output, not a live execution result.

7

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
invalid use of undefined type `struct S`Struct only forward-declared before `p->field`-Wall -HInclude full `struct S { ... };` definition first
unknown type `Node` inside struct bodySelf-pointer used typedef alias too early-WpedanticWrite `struct Node *next;` inside `typedef struct Node`

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

Check Header Includes
gcc -std=c17 -Wall -Wextra -H main.c -o app

Lists header inclusion order to reveal missing or cyclic struct headers.

Typedef Order Audit
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.