offsetof() operator. A tricky question.
amos shapira
amoss at batata.huji.ac.il
Tue Mar 13 16:08:13 AEST 1990
vohra at uts.amdahl.com (Pavan Vohra) writes:
>I would like to compile in the offsets of structure members.
>For example, with this structure
> struct astruct {
> int member0;
> char member1;
> } myastruct;
>I want to have, in the same program that the structure appears in,
>the number of bytes past myastruct where I can find member1.
The answer could be simple and I used several times, do this:
struct astruct *ap;
ap = NULL;
offsetof_member0 = &ap->member0; /* zero offset */
offsetof_member1 = &ap->member1; /* sizeof(member0) */
An offsetof() operator would be quite handy here. But until then you can
use this with a good portability of your programs.
Now comes to mind, maybe you could avoid the nbull pointer variable with this
(didn't try it):
offsetof_member0 = &((struct astruct *)NULL->member0);
offsetof_member1 = &((struct astruct *)NULL->member1);
- Amos Shapira
amoss at batata.bitnet
amoss at batata.huji.ac.il
More information about the Comp.lang.c
mailing list