structure references in header files

der Mouse mouse at mcgill-vision.UUCP
Fri Dec 13 16:15:32 AEST 1985


> 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



More information about the Comp.lang.c mailing list