Mail Archives: djgpp/1999/01/26/00:59:55
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 -