Others have answered the specific question; this is more general:
In article <yg*************@janus.isnogud.escape.de>,
Urs Thuermann <ur*@isnogud.escape.dewrote:
>With offsetof() I can get the offset of a member in a struct. AFAICS,
it is portable and clean to use this offset to access that member. ...
A structure member, inside a C compiler, really provides two items:
an offset, and a type. Given those two items -- offset and type --
you can "synthesize" a member-access.
The C syntax "structure.member" essentially means "using the
structure named on the left, which should be an lvalue[%], find
the address of the object implied by that lvalue, add the offset
implied by the member, and access an object whose type is
determined by the member." This rule also works for unions,
where the offset is always zero.
The "offset add" must be done in terms of "bytes" (C bytes, i.e.,
"unsigned char"s), of course. (&structure)+1, using ordinary
(i.e., entire-object-size) pointer arithmetic, points right
past the entire structure, so any nonzero offset would be too
big if this "offset add" were not "de-scaled" down to byte size.
-----
[%] This deliberately ignores the special case of a function return
value of structured type, which has some funny constraints. In
particular, such a return value is the only way to produce an
"array rvalue", which has no address, yet can still be subscripted
to access an array element. If you somehow access the (nonexistent)
address of a struct-type return value or any of its members --
including an array or an array element -- the effect is not defined.
It is generally unwise even to try, whether or not the compiler
catches you. :-)
-----
The C syntax "expr->member" essentially means "using the pointer
value given on the left, and the offset and type implied by
the member, access the object at that offset." This is of course
identical to "(*(expr)).member" since *(expr) produces the lvalue
pointed-to by the expression. Taking its address and adding the
offset has the same effect as just starting with the expression,
treating it as an address, and adding the offset.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.