3.7.2 External Object Definitions

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Thu Feb 7 09:47:13 AEST 1991


Say struct foo's first element points to struct bar, and vice versa.
Here's how to initialize mutually referencing static structures:

  static struct {
    struct foo f;
    struct bar b;
  } x = {
    { &x.b },
    { &x.f }
  };

Done. Most compilers will use fixed addresses for &x.b and &x.f, so this
will be just as efficient as separate static structure declarations.
  
In article <eyal.665840220 at echo> eyal at echo.canberra.edu.au (Eyal Lebedinsky) writes:
> It is obvious that the second object defined should first have a
> forward declaration for the first object to be properly initialized.

No need.

> I know how shady this area is with traditional compilers and do not wish to
> discuss it, so please limit replies to ANSI issues.

No, the solution above works fine under any C compiler.

This is the fourth time since September that the same question has come
up in comp.lang.c (and now comp.std.c); my fingers are getting tired.
Anyone else think this belongs in the comp.lang.c FAQ? Steve, are you
listening?

---Dan



More information about the Comp.std.c mailing list