Mail Archives: djgpp/1996/11/12/01:40:12
On 11 Nov 1996, Brennan The Rev. Bas Underwood wrote:
> Since a struct is just a pile of memory, you need to know the location
> of the beginning of the struct and the offset of the element you want.
> The difficulty of this goes up (slightly) since gcc likes to pad structures,
> so you can't just count bytes (unless you __attribute__((packed)) everything,
> which is going to slow down your accesses, which asm won't compensate
> for, so don't do that.)
>
> In C you can get the address of a struct element via &structname.element.
> You can get the offset in bytes via
> ((int)&structname.element - (int)&structname)
There's no need to go to these lengths to compute offsets of struct
fields. Just use the `offsetof' macro; it's ANSI C.
- Raw text -