Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mcgill-vision.UUCP Path: utzoo!watmath!clyde!burl!ulysses!gamma!epsilon!zeta!sabre!petrus!bellcore!decvax!mcnc!philabs!micomvax!musocs!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: net.lang.c Subject: Re: structure references in header files Message-ID: <343@mcgill-vision.UUCP> Date: Fri, 13-Dec-85 01:15:32 EST Article-I.D.: mcgill-v.343 Posted: Fri Dec 13 01:15:32 1985 Date-Received: Mon, 16-Dec-85 05:32:06 EST References: <531@brl-tgr.ARPA> Organization: McGill University, Montreal Lines: 67 > I have a problem with a header file that contains typedefs similar > to those below in that both contain a reference to the other. > [example, similar to:] > typedef struct a { struct b *b; } A; > typedef struct b { struct a *a; } B; Well, what I would do is struct a { struct b { struct a *a; } *b; }; typedef struct a A; typedef struct b B; I don't believe you can make it recognize forward references in general. You certainly can't do it for direct elements, because the compiler doesn't know the size of the member, and you can't do it for pointers because architectures might (do?) exist out there for which not all pointers are the same size (yes, I too think anyone who designs such a machine as "general purpose" should be forced to write the C compiler for it themselves; it might teach them something). So you certainly can't forward reference the typedefs. On some machines you might be able to forward reference the structure, provided you use a pointer to it (does X3J11 have anything to say about this?). Someone mentioned that a kludge had been put in so you could get around this with struct b; struct a { struct b *b; }; struct b { struct a *a; }; Ugh. Speaking of structures containing self-referential pointers, I once wrote some code which used the following structures: struct _coord { struct _coord *flink; struct _coord *blink; int val; int index; struct _boxlist { struct _boxlist *link; struct _box { struct _box *flink; struct _box *blink; int number; struct _coord *lx; struct _coord *ux; struct _coord *ly; struct _coord *uy; } *box; } *boxes; }; Three structures, a total of 14 structure elements, and only three elements which aren't just a pointer to another structure. (Okay, okay, I'll put it in net.jokes.c next time, I promise...) -- der Mouse USA: {ihnp4,decvax,akgua,etc}!utcsri!mcgill-vision!mouse philabs!micomvax!musocs!mcgill-vision!mouse Europe: mcvax!decvax!utcsri!mcgill-vision!mouse mcvax!seismo!cmcl2!philabs!micomvax!musocs!mcgill-vision!mouse Hacker: One who accidentally destroys / Wizard: One who recovers it afterward