Path: utzoo!utgpu!water!watmath!clyde!rutgers!umd5!trantor.umd.edu!chris From: chris@trantor.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Forward reference for structure? Message-ID: <2295@umd5.umd.edu> Date: 14 Feb 88 03:21:54 GMT References: <173@heurikon.UUCP> <341@hub.ucsb.edu> <359@hub.ucsb.edu> Sender: ris@umd5.umd.edu Reply-To: chris@trantor.umd.edu (Chris Torek) Organization: University of Maryland, College Park Lines: 51 >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