What is forward reference in C?

后端 未结 6 1863

What is forward reference in C with respect to pointers?

Can I get an example?

6条回答
  •  渐次进展
    2020-12-14 13:41

    Adding to previous answers. The typical situation in which forward reference is mandatory is when a struct foo contains a pointer to a struct bar, and bar contains a pointer to foo (a circular dependency between declarations). The only way to express this situation in C is to use a forward declaration, i.e.:

    struct foo;
    
    struct bar
    {
       struct foo *f;
    };
    
    struct foo
    {
       struct bar *b;
    };
    

提交回复
热议问题