structure element offsets

Stuart D. Gathman stuart at bms-at.UUCP
Wed Nov 26 12:17:48 AEST 1986


In article <1096 at spice.cs.cmu.edu>, bader at 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>



More information about the Comp.lang.c mailing list