initializing static mutually referencing structures

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Sat Sep 22 13:25:31 AEST 1990


In article <186 at thor.UUCP> scjones at thor.UUCP (Larry Jones) writes:
> In article <1287 at granjon.UUCP>, jhpb at granjon.UUCP (Joseph H. Buehler) writes:
> > I have some structures that refer to each other, that I want to be
> > static.  The compiler doesn't like the following because it doesn't know
> > what y is when it's initializing x:
> > 	static struct xtag x = {&y};
> > 	static struct ytag y = {&x};

A different approach should work under any C compiler, though it may not
be implemented efficiently. It also has the usual syntactic problems of
macros.

  static struct { struct xtag x; struct ytag y; }
    shmoe = { &(shmoe.y), &(shmoe.x) } ;
  #define x (shmoe.x)
  #define y (shmoe.y)

Caveat: This is untested. But the idea is useful to remember.

---Dan



More information about the Comp.lang.c mailing list