🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2369

Re: 3.7.2 External Object Definitions

#2369
From: Dan Bernstein
Date: Wed, 06 Feb 1991 22:47
31 lines
1476 bytes
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@echo> eyal@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
Technical Details
Message-ID: <14327:Feb622:47:1391@kramden.acf.nyu.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!sun-barr!rutgers!cmcl2!kramden.acf.nyu.edu!brnstnd
References: <eyal.665840220@echo>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!sun-barr!rutgers!cmcl2!kramden.acf.nyu.edu!brnstnd
From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein)
Newsgroups: comp.std.c
Subject: Re: 3.7.2 External Object Definitions
Message-ID: <14327:Feb622:47:1391@kramden.acf.nyu.edu>
Date: 6 Feb 91 22:47:13 GMT
References: <eyal.665840220@echo>
Organization: IR
Lines: 31