home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!goanna!ok
- From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
- Newsgroups: comp.programming
- Subject: Re: Mutual reference in C?
- Message-ID: <15886@goanna.cs.rmit.oz.au>
- Date: 11 Nov 92 05:02:47 GMT
- References: <1djb0tINNa9t@morrow.stanford.edu>
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Lines: 36
-
- In article <1djb0tINNa9t@morrow.stanford.edu>, ehh@camis.Stanford.EDU (Edward H. Herskovits) writes:
- > simple question to which i couldn't find an answer in standard c books:
- Are you sure? It's really something very fundamental.
- > if there are two structures, a and b, that refer to each other,
- > ...
- > i know that in pascal (ugh) i could use the "forward" keyword; what
- > would be the equivalent thing to do in c?
-
- No, "forward" in Pascal has nothing to do with structs.
-
- The method for declaring mutually recursive data types in C and Pascal
- is essentially the same. The mutual references must involve pointers,
- so
- - first declare all the pointer types you need
- - then declare the other types, using the pointer types.
-
- In Pascal, you can use ^Type before Type has been defined.
- In C, you can use struct Tag before Tag has been defined.
-
- Pascal:
- type
- aPtr = ^aRec;
- bPtr = ^bRec;
- aRec = record ... b: bPtr; ... end;
- bRec = record ... a: aPtr; ... end;
-
- C:
- typedef struct aRec *aPtr;
- typedef struct bRec *bPtr;
- struct aRec { ... bPtr b; ... };
- struct bRec { ... aPtr a; ... };
-
- You _can_ eliminate the typedefs in C, but it is usually handy to have
- them around, because a program that manipulates these things is likely
- to have a lot of variables of these pointer types.
-
-