[comp.lang.c] struct a <---> struct b

lampman@heurikon.UUCP (Ray Lampman) (02/06/88)

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@heurikon.UUCP)

rk9005@cca.ucsf.edu (Roland McGrath) (02/07/88)

["struct a <---> struct b"] - lampman@heurikon.UUCP (Ray Lampman):
} What is the best way to declare three different structures,
} each containing a pointer to the other two?
} I keep running into a forward referencing error.
} I'm looking for a solution without typedef's, I'll add those later.
} aTdHvAaNnKcSe, Ray.
} -- 
}                                         - Ray Lampman (lampman@heurikon.UUCP)

Try:
	struct a;
	struct b;
	struct c;
	struct a { struct b *bp; struct c *cp; };
	struct b { struct a *ap; struct c *cp; };
	struct c { struct a *ap; struct b *bp; };

-- 
		Roland McGrath
UUCP: 			...!ucbvax!lbl-rtsg.arpa!roland
ARPA:	 		roland@rtsg.lbl.gov

angst%csilvax@hub.ucsb.edu (The Stinkpuff) (02/07/88)

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

I'm not sure what you mean by a forward referencing error.  Since all
pointers are the same size, the compiler shouldn't care if it doesn't
yet know the object that is being pointed to.  The following program
compiles fine on my machine (a VAX running 4.3BSD) --

struct a {
    struct b *bp;		/* forward reference */
    struct c *cp;		/* ditto */
    int other_stuff;
};

struct b {
    struct a *ap;
    struct c *cp;		/* forward reference */
    int other_stuff;
};

struct c {
    struct a *ap;
    struct b *bp;
    int other_stuff;
};

main ( )
{
}

Dave Stein						    "Life's a clambake."
ARPA: angst%csilvax@hub.ucsb.edu	      Disclaimer: college students don't
UUCP: ...ucbvax!hub!csilvax!angst			  need no disclaimers!

gwyn@brl-smoke.ARPA (Doug Gwyn ) (02/08/88)

In article <173@heurikon.UUCP> lampman@heurikon.UUCP (Ray Lampman) writes:
>What is the best way to declare three different structures,
>each containing a pointer to the other two? I keep running
>into a forward referencing error.

struct b;	/* incomplete types for forward reference */
struct c;

struct a { struct b *bp; struct c *cp; } as;
struct b { struct c *cp; struct a *ap; } bs;
struct c { struct a *ap; struct b *bp; } cs;

jrl@anuck.UUCP (j.r.lupien) (02/10/88)

In article <173@heurikon.UUCP>, lampman@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@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

al@gtx.com (0732) (02/10/88)

>What is the best way to declare three different structures, each containing a pointer
>to the other two? I keep running into a forward referencing error. I'm looking

Very strange. What compiler is this please, running on what machine?

(Sorry for the posting, I lost the address)
    ----------------------------------------------------------------------
   | Alan Filipski, GTX Corp, 2501 W. Dunlap, Phoenix, Arizona 85021, USA |
   | {ihnp4,cbosgd,decvax,hplabs,amdahl}!sun!sunburn!gtx!al (602)870-1696 |
    ----------------------------------------------------------------------

Alan_T._Cote.OsbuSouth@Xerox.COM (02/11/88)

Finally -- one I can answer!!

>What is the best way to declare three different structures, each containing a pointer
>to the other two? I keep running into a forward referencing error.

Try declaring all three as externals ahead of time.  Try the following:

    struct s
      {
      struct s *p0;
      struct s *p1;
      };

    extern struct s a;
    extern struct s b;
    extern struct s c;

    struct s a = { &b, &c };
    struct s b = { &a, &c };
    struct s c = { &a, &b };

How's that?!?

lvc@tut.cis.ohio-state.edu (Lawrence V. Cipriani) (02/15/88)

In article <11774@brl-adm.ARPA> Alan_T._Cote.OsbuSouth@Xerox.COM writes:
>Finally -- one I can answer!!
>
>>What is the best way to declare three different structures, each containing a pointer
>>to the other two? I keep running into a forward referencing error.
>
>Try declaring all three as externals ahead of time.  Try the following:

The way I took this question was that the different structures are also of
different type.  If this is the case the answer is different.  Do the
following:

	struct a;	/* Hey compiler! a b and c are structures */
	struct b;	/* that I will define later */
	struct c;

	struct a
	{
		struct b *pb;
		struct c *pc;
		...	/* other stuff */
	};

	struct b
	{
		struct a *pa;
		struct c *pc;
		...	/* other stuff */
	};

	struct c
	{
		struct b *pb;
		struct c *pc;
		...	/* other stuff */
	};

The initialization of the pointers is left as an exercise for the
interested reader.

-- 
oo
Larry Cipriani, AT&T Networks Systems (by day) Ohio State University (by night)
Domain: lvc@tut.cis.ohio-state.edu
Path: ...!cbosgd!osu-cis!tut.cis.ohio-state.edu!lvc (yes its right)

lvc@tut.cis.ohio-state.edu (Lawrence V. Cipriani) (02/15/88)

In article <6573@tut.cis.ohio-state.edu> lvc@tut.cis.ohio-state.edu

>	struct c
>	{
>		struct b *pb;
>		struct c *pc;
>		...	/* other stuff */
>	};

Oops!  The line mangler strikes again.  Should be:

	struct c
	{
		struct a *pa;
		struct b *pb;
		...	/* other stuff */
	};

I should have mentioned that this technique is mentioned in K&R and isn't
a local hack to the C compiler.

-- 
oo
Larry Cipriani, AT&T Networks Systems (by day) Ohio State University (by night)
Domain: lvc@tut.cis.ohio-state.edu
Path: ...!cbosgd!osu-cis!tut.cis.ohio-state.edu!lvc (yes its right)