Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!vrdxhq!dgis!bms-at!stuart From: stuart@bms-at.UUCP (Stuart D. Gathman) Newsgroups: comp.lang.c Subject: Re: structure element offsets Message-ID: <292@bms-at.UUCP> Date: Tue, 25-Nov-86 21:17:48 EST Article-I.D.: bms-at.292 Posted: Tue Nov 25 21:17:48 1986 Date-Received: Wed, 26-Nov-86 03:40:37 EST References: <1096@spice.cs.cmu.edu> Organization: Business Management Systems, Inc., Fairfax, VA Lines: 26 Keywords: structure, offset Summary: Structure offsets and other goodies In article <1096@spice.cs.cmu.edu>, 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? I have --------spos.h--------------------------- /* structure info macros spos - member offset sposa - array member offset (since &array often illegal) slen - member size smp - convert pointer to member to pointer to enclosing structure smpa - smp for array member NOTE - these macros assume sizeof(char) == 1. If the nerds win and we have to do it, this file needs to be fixed. This file can also be tweaked for stupid compilers that like to evaluate constants at run time. */ #define spos(s,m) ((char *)&((struct s *)0)->m - (char *)0) #define sposa(s,m) ((char *)((struct s *)0)->m - (char *)0) #define slen(s,m) (sizeof((struct s *)0)->m) #define smp(s,m,p) ((struct s *)(p-&((struct s *)0)->m)) #define smpa(s,m,p) ((struct s *)(p-((struct s *)0)->m)) -- Stuart D. Gathman <..!seismo!dgis!bms-at!stuart>