home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / programm / 3104 < prev    next >
Encoding:
Internet Message Format  |  1992-11-10  |  1.6 KB

  1. Path: sparky!uunet!munnari.oz.au!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.programming
  4. Subject: Re: Mutual reference in C?
  5. Message-ID: <15886@goanna.cs.rmit.oz.au>
  6. Date: 11 Nov 92 05:02:47 GMT
  7. References: <1djb0tINNa9t@morrow.stanford.edu>
  8. Organization: Comp Sci, RMIT, Melbourne, Australia
  9. Lines: 36
  10.  
  11. In article <1djb0tINNa9t@morrow.stanford.edu>, ehh@camis.Stanford.EDU (Edward H. Herskovits) writes:
  12. > simple question to which i couldn't find an answer in standard c books:
  13. Are you sure?  It's really something very fundamental.
  14. > if there are two structures, a and b, that refer to each other,
  15. > ...
  16. > i know that in pascal (ugh) i could use the "forward" keyword; what
  17. > would be the equivalent thing to do in c?
  18.  
  19. No, "forward" in Pascal has nothing to do with structs.
  20.  
  21. The method for declaring mutually recursive data types in C and Pascal
  22. is essentially the same.  The mutual references must involve pointers,
  23. so
  24.     - first declare all the pointer types you need
  25.     - then declare the other types, using the pointer types.
  26.  
  27. In Pascal, you can use ^Type before Type has been defined.
  28. In C, you can use struct Tag before Tag has been defined.
  29.  
  30. Pascal:
  31.     type
  32.         aPtr = ^aRec;
  33.         bPtr = ^bRec;
  34.         aRec = record ... b: bPtr; ... end;
  35.         bRec = record ... a: aPtr; ... end;
  36.  
  37. C:
  38.     typedef struct aRec *aPtr;
  39.     typedef struct bRec *bPtr;
  40.     struct aRec { ... bPtr b; ... };
  41.     struct bRec { ... aPtr a; ... };
  42.  
  43. You _can_ eliminate the typedefs in C, but it is usually handy to have
  44. them around, because a program that manipulates these things is likely
  45. to have a lot of variables of these pointer types.
  46.  
  47.