[comp.lang.c] Forward reference for structure?

angst%csilvax@hub.ucsb.edu (The Stinkpuff) (02/13/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)

Sorry for beating a dead horse, but I'm confused about this forward
referencing bit.  I posted a response to this which included what I
thought was a solution (because my program compiles *without* doing
anything special w/regard to forward referencing), but the solutions
posted by other people all do something to avoid forward referencing.

Here is my program, compressed to save space:

struct a { struct b *bp;  struct c *cp;  int other_stuff; };
struct b { struct a *ap;  struct c *cp;  int other_stuff; };
struct c { struct a *ap;  struct b *bp;  int other_stuff; };
main ( ) { }

[This program compiles and runs fine on a VAX running 4.3BSD.]

Can someone please enlighten me as to why there seems to be such a
problem with this?  The solutions I have seen so far have included
defining "struct b" and "struct c" as "dummy" structures, or defining
them as externs, prior to the declaration of "struct a."  Since I
did not have to go this trouble, could someone please enlighten me
as to what's going on?  Chris?  Someone?  Anyone...

(If anyone would like to respond, why don't you email to me, and I
will post a summary of the responses I receive.  AdvTHANKSance.)

Dave Stein                     Running without aim through the razor weeds that
angst%csilvax@hub.ucsb.edu     only reach my knees.   And when I'm lying in the 
...ucbvax!hub!csilvax!angst    grey sleep, I don't know how to walk the boards.

chris@trantor.umd.edu (Chris Torek) (02/14/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?

In article <359@hub.ucsb.edu> angst%csilvax@hub.ucsb.edu writes:
>... I'm confused about this forward referencing bit.  I posted a
>response to this which included what I thought was a solution
>(because my program compiles *without* doing anything special
>w/regard to forward referencing), ....
>
>Can someone please enlighten me as to why there seems to be such a
>problem with this?

There is nothing illegal about

	% cat file.c
	struct goo { struct foo *fp; int gooval; };
	struct foo { struct goo *gp; int fooval; };
	%

The problem that occurs is when the same name is redefined, viz:

	struct global { int v; };
	f() {
		struct local { struct global *gp; } l;
		struct global /* not really */ { char *s; } oops;

The (struct global *) element of l called `gp' is a pointer to the
`really global' structure; the only member of this is `v'.  l.gp
cannot be made to point to `oops', even though it is an object of
type `struct global', because it is a different `struct global'.

The dpANS provides a way of telling the compiler `forget about any
outer definitions of this structure, because I am going to redefine
it at this nesting level'.  Changing the code to

	struct global { int v; };
	f() {
		struct global;
		struct local { struct global *gp; } l;
		struct global /* not really */ { char *s; } oops;

makes it Officially Correct.  Of course, it still does not compile
under PCC-based compilers (4.3BSD at least).

I will make no comment as to whether redefining structures locally
is a good idea.
-- 
In-Real-Life: Chris Torek, Univ of MD Computer Science, +1 301 454 7163
(hiding out on trantor.umd.edu until mimsy is reassembled in its new home)
Domain: chris@mimsy.umd.edu		Path: not easily reachable