offsetof() operator. A tricky question.

Tim W Smith ts at cup.portal.com
Sun Mar 18 21:44:01 AEST 1990


< 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.

It IS simple.  It is also wrong.

If you want to have at least a small chance of working across different
machines, try this:

	offsetof_member0 = (ulong)&ap->member0 - (ulong)ap;
	...
						Tim Smith



More information about the Comp.lang.c mailing list