delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/26/00:59:55

Sender: nate AT cartsys DOT com
Message-ID: <36AD59D1.8CB03C29@cartsys.com>
Date: Mon, 25 Jan 1999 21:59:45 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.0.36 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Strange pointer manipulation
References: <36AD11AA DOT 3876 AT erols DOT com>
Reply-To: djgpp AT delorie DOT com

John S. Fine wrote:
> 
> I want a decent way to make a macro, which I call undot,
> that does something like the reverse of the "." operator.
[delete application note]
>   The heart of the problem is representing the offset of b
> within FOO in some reasonable GCC syntax.  Clearly the
> compiler knows the value, since it uses it every time you
> do something like (xxx->b).  But, I don't know any good
> way to represent it.
> 
>   I don't need portable C;  A GCC specific kludge would be
> fine.  Using a GCC specific kludge, I came close:
[delete example]
>   Is there some C or GCC feature that I am overlooking that
> provides an easier way?

Yes; the `offsetof' macro.  `offsetof(type, member)' equals the offset
of `member' within `type'.  So

struct foo { int a, b, c; };
offsetof(struct foo, b)
   => 4

It's in <stddef.h>, so I assume it's ANSI.  Interestingly, GCC defines
it as

((size_t) &((TYPE *)0)->MEMBER)

IOW, the address of MEMBER of a TYPE at address 0.
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019