Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!iuvax!cica!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!shelby!csli!poser From: poser@csli.Stanford.EDU (Bill Poser) Newsgroups: comp.lang.c Subject: Re: How to get a byte offset Message-ID: <13824@csli.Stanford.EDU> Date: 28 May 90 18:51:51 GMT References: <1990May28.034643.6962@cs.umn.edu> <16802@phoenix.Princeton.EDU> Sender: poser@csli.Stanford.EDU (Bill Poser) Reply-To: poser@csli.stanford.edu (Bill Poser) Distribution: usa Organization: Center for the Study of Language and Information, Stanford U. Lines: 25 In article <16802@phoenix.Princeton.EDU> pfalstad@phoenix.Princeton.EDU (Paul John Falstad) writes: > >The first way is just to add up all the sizeof values of a, b, and c. > > #define D_OFFSET (sizeof(int)+sizeof(char)*3+sizeof(int)*10) This won't work in general since, in order to satisfy alignment requirements, a struct may contain padding bytes. For example, on some machines the structure: struct foo{ char b; short a; }; will contain 4 bytes because it will be laid out in memory like this: 0000 b 0001 PADDING 0002 low byte of a 0003 high byte of a Without the padding a would not begin on an even address, which is not permitted in some architectures. Bill