Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!rutgers!pyrnj!mirror!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: structure element offsets Message-ID: <186@haddock.UUCP> Date: Thu, 4-Dec-86 20:42:53 EST Article-I.D.: haddock.186 Posted: Thu Dec 4 20:42:53 1986 Date-Received: Fri, 5-Dec-86 06:06:54 EST References: <1096@spice.cs.cmu.edu> <292@bms-at.UUCP> <720@dg_rtp.UUCP> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Organization: Interactive Systems, Boston Lines: 21 Summary: It's available in ANSI In article <720@dg_rtp.UUCP> throopw@dg_rtp.UUCP (Wayne Throop) writes: >>bader@spice.cs.cmu.edu (Miles Bader) writes: >>> Is there any way of finding the offset of a structure element from >>> the beginning of a structure in a portable AND efficient way? >stuart@bms-at.UUCP (Stuart D. Gathman) writes: >> #define spos(s,m) ((char *)&((struct s *)0)->m - (char *)0) > >... A couple of points remain. First, there is no guarantee that pointer >arithmetic or offset calculations will work for the null pointer. Note also that the result might not be a legal constant. However, ANSI seems to have its collective heart set on making offsetof() part of the standard, which means that on machines where the above construct doesn't work, *some* hook must be allowed -- even if the implementor has to make "offsetof" a builtin operator instead of a macro. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint (Btw, "sposa(s,m)" is redundant since "spos(s,m[0])" will do, and the macro would be more useful if the word "struct" were omitted.)