finding offset of a member in C structures

David Brooks dbrooks at osf.org
Thu May 23 00:48:29 AEST 1991


In article <16220 at smoke.brl.mil>, gwyn at smoke.brl.mil (Doug Gwyn) writes:
|> In article <22051 at paperboy.OSF.ORG> dbrooks at osf.org (David Brooks) writes:
|> >The method you posted:
|> >	struct a_struct foo;
|> >	int offset2 = (char *)&foo.member2 - (char *)&foo; /* for example */
|> >has an unfortunate drawback.  You need to be able to declare foo, and
|> >squander space for it.  The ubiquitous uses in X don't have those luxuries.
|> 
|> No, you don't.  If you understood my example, you should be able to apply
|> the technique whenever you can latch onto, for example, a pointer to a
|> struct of the appropriate type.

As I said, the ubiquitous uses in X don't have that luxury.

|> The X11 source code style is ATROCIOUS and should not be used as a model.

Oh, I agree with that.  Looking at the macro, I'm always astounded that it
works.

However, the fact is, in two years of supporting folks using X, I've never
come across a pre-ANSI compiler that failed to handle the X macro.  And,
for ANSI compilers, with an official offsetof, it's moot.
-- 
David Brooks				dbrooks at osf.org
Systems Engineering, OSF		uunet!osf.org!dbrooks



More information about the Comp.lang.c mailing list