Re^3: offsets in structures.

M. J. Shannon, Jr. mjs at rabbit.UUCP
Mon Oct 22 03:55:27 AEST 1984


How about this fragment for a `portable' method of computing structure
offsets:

	f(sp)
	struct stuff *sp;
	{
		int	offset; /* may need to be long for some */

		offset = (char *) &sp->member - (char *) sp;
	}

If a (valid) pointer to the structure in question is not available,
it is easy (if costly on some machines) to make an auto:

	f()
	{
		struct stuff local_stuff;
		int	offset;	/* may need to be long... */

		offset = (char *) &local_stuff.member - (char *) &local_stuff;
	}

To my knowledge, this is the `ultimate' in portable offset calculation.
I would be extremely interested in hearing about any machines on which
either fragment fails to store the correct offset in `offset'.

	Yours in portability (just west of Outer Slobovia),
-- 
	Marty Shannon
UUCP:	{alice,rabbit,research}!mjs
	(rabbit is soon to die.  Does this mean alice is pregnant?  Yup!)
Phone:	201-582-3199



More information about the Comp.lang.c mailing list