home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11745 < prev    next >
Encoding:
Internet Message Format  |  1992-07-29  |  2.1 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!rbiffm!math.uni-frankfurt.de!lingnau
  2. From: lingnau@math.uni-frankfurt.de (Anselm Lingnau)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records v. Unions
  5. Message-ID: <3097@rbiffm.informatik.uni-frankfurt.de>
  6. Date: 30 Jul 92 08:56:17 GMT
  7. References: <1992Jul15.212536.3127@otago.ac.nz>
  8. Sender: news@rbiffm.informatik.uni-frankfurt.de
  9. Distribution: world
  10. Organization: University of Frankfurt/Main, Dept. of Mathematics
  11. Lines: 45
  12. Nntp-Posting-Host: gauss.math.uni-frankfurt.de
  13.  
  14. In article <1992Jul15.212536.3127@otago.ac.nz>, gpxpost@otago.ac.nz writes:
  15. > [...]
  16. > Solution 1.
  17. > #define base  u.pointer_or_file_or_set.base
  18. > etc...
  19. > So lhsType->base expands to lhsType->u.pointer_or_file_or_set.base as
  20. > required.  But now we're scared of clobbering any other variables called
  21. > base.  So we need to use type name prefix thingies:
  22. > #define typ_base  u.pointer_or_file_or_set.base
  23. > and use lhsType->typ_base.  But I hate typ_ even more than u.stuff.base.
  24. > [...]
  25. > Solution 3.
  26. > This is where you fill in the blanks.  Anyone got any suggestions?
  27.  
  28. I often use `access macros' defined like
  29.  
  30. #define Kind(t)        ((t)->kind)
  31. #define PFSBase(t)    ((t)->u.pointer_or_file_or_set.base)
  32. #define PFSLow(t)    ((t)->u.pointer_or_file_or_set.low)
  33.  
  34. Your example thus becomes
  35.  
  36. lhstype = translateExpr(node->lhs);
  37. if (Kind(lhsType) == Pointer)
  38.     return PFSBase(lhsType);
  39. if (Kind(lhsType) == Array)
  40.     CheckBounds(rhs, Index(lhsType));
  41.  
  42. A minor drawback is that the macros assume you will be accessing the struct
  43. through a pointer, although it would be easy to define them with . in place
  44. of -> and use *lhsType or some such when they are called. But that doesn't
  45. look as neat, and normally you can make out beforehand whether you'll be
  46. mostly using pointers or direct access. Actually this notation is similar
  47. to the one Knuth uses for structures in TAoCP.
  48.  
  49. Anselm
  50. --
  51. Anselm Lingnau, Buchenweg 1, 6239 Eppstein| You see things, and you say `Why?'
  52. lingnau@math.uni-frankfurt.de      Germany| But I dream things that never were,
  53. University of Frankfurt, CompSci and Maths| and say `Why not?'  --- G. B. Shaw
  54.