Forward reference for structure?
Chris Torek
chris at trantor.umd.edu
Sun Feb 14 13:21:54 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 pointer to the other two?
In article <359 at hub.ucsb.edu> angst%csilvax at 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 at mimsy.umd.edu Path: not easily reachable
More information about the Comp.lang.c
mailing list