Only declare an struct

That's a forward declaration, not a definition — it just tells the compiler "there's a type named struct pswitch, details unknown yet." It's legal C as long as you only use it in ways that don't need to know the struct's size or members.

Bash Find all uses of pswitch in ntb.h
IN
grep -n "pswitch" /home/msong/wk/ntb_sim/src/ntb.h
OUT
158:struct pswitch;
172:    struct pswitch    *pswitch;

That confirms it: struct pswitch is only ever used as a pointer (struct pswitch *pswitch; at line 172), never dereferenced or embedded by value in this header.

That's why it compiles fine:

  • A pointer to an incomplete type is legal — the compiler just needs to know pointers are (say) 8 bytes, which it always knows regardless of what pswitch contains.
  • What forward declarations can't do is let you embed the struct by value (struct pswitch p;) or access its members (p->field), since the compiler doesn't know the size/layout yet.

The real definition of struct pswitch presumably lives in pswitch.h or pswitch.c, and any .c file that needs to dereference ntb->pswitch will #include that header to get the full definition. This is the standard C idiom for breaking circular header dependencies (e.g., ntb.h and pswitch.h likely reference each other's types) and

posted on 2026-09-21 15:19  ENGINEER-F  阅读(6)  评论(0)    收藏  举报