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