Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!mcvax!ukc!eagle!icdoc!cam-cl!am From: am@cl.cam.ac.uk (Alan Mycroft) Newsgroups: comp.lang.c Subject: offsetof (was Arithmetic on NULL) Message-ID: <1076@jenny.cl.cam.ac.uk> Date: Mon, 12-Oct-87 07:00:38 EDT Article-I.D.: jenny.1076 Posted: Mon Oct 12 07:00:38 1987 Date-Received: Thu, 15-Oct-87 20:08:18 EDT References: <1459@cullvax.UUCP> <48400001@tub.UUCP> <1151@haddock.ISC.COM> Reply-To: am@cl.cam.ac.uk (Alan Mycroft) Organization: U of Cambridge Comp Lab, UK Lines: 20 In article <1151@haddock.ISC.COM> karl@haddock.ima.isc.com (Karl Heuer) wrt: >>I see some benign applications for constant expressions involving null >> (char *)&((struct foo *)0)->bar - (char *)&((struct foo *)0)->baz >Now, it may well be the case that "offsetof" is defined in to >expand into the above mess, on machines where it happens to work. That's >okay; the use is portable even though the implementation is not. I have a concern about this. ANSI (Oct 86) specifically disallows unary-& from appearing in constant expressions (no doubt except in sizeof() context). Thus offsetof cannot expand into the above mess since it has to be a constant expression, e.g. for initialisers. My current hack is to take the view that &((struct foo *)0)->bar) is a constant expression (pace ANSI) to allow offsetof() to be definable by macro at all. Apart from the arguably worse suggestion that offsetof expands into 'funny' reserved words beginning with '_' does anyone have a ANSI-legal version of offsetof() for any machine (e.g. vax-like)? I suppose I reckon it is impossible, but requires much legalistic reading of dpANS.