struct a <---> struct b

j.r.lupien jrl at anuck.UUCP
Wed Feb 10 06:36:41 AEST 1988


In article <173 at heurikon.UUCP>, lampman at heurikon.UUCP (Ray Lampman) writes:
> What is the best way to declare three different structures, each containing a po
> inter to the other two? I keep running into a forward referencing error. I'm loo
> king for a solution without typedef's, I'll add those later. aTdHvAaNnKcSe, Ray.
> -- 
>                                         - Ray Lampman (lampman at heurikon.UUCP)

Hi, Ray,
   It'll be a miracle if you get this, with a path that long.
So, I'm also posting it to the net, on the offchance that this
will help other C programmers.

Anyway, I've done this for two structs, as follows:

struct _foo
{
	struct _bar
	{
		struct _foo *fooptr;
		other_members_of_the_bar;
	} *barptr;
	other_foo_members;
} the_foo;

This works, I've used it with great success. Now, to expand the 
beast out to three levels, let's try:

struct _foo
{
	struct _bar
	{
		struct _zot
		{
			struct _foo *fooptr;
			struct _bar *barptr;
			other_zots;
		} *zotptr;
		struct _foo *fooptr;
		other_bars;
	} *barptr;
	struct _zot *zotptr;
	other_foos;
} one_such_foo;

Hang on a second, I'll try it:

Indeed, this seems to win the chocolate covered milk biscuit.

By all means, use this trick in good health. Credit to Pablo
Halpern for the two-struct idea (good times at DCI).

			-John Lupien
			ihnp4!mvuxa!anuxh!jrl



More information about the Comp.lang.c mailing list