[net.lang.c] proposed new construct, offsetof

padpowell@wateng.UUCP (PAD Powell) (10/24/84)

I have just been burned again, by a C compiler, and a structure.

I propose the following construct to be added to C:

offsetof( structure_name.field.field.... )

Given the name of the stucture, and the list of the fields, returns the
integer value of the offset from the beginning of the structure, that
will be used in accessing the particular field.

1.  This is useful in DEBUGGING, and finding if you have portable code.
	I have just been burned with 16/32 bit integers.
2.  It provides a "hook" into the compiler which will eliminate all the
	ugly arguements about "offset calculations using 0 pointers"
	which is raging.
3.  In my personal opinion, it is a solution to a problem that requires
	looking at object code, and then guessing what the offset is.

Patrick ("Whimper! first the optimizer gets me, now the structures")
	Powell

marcus@pyuxt.UUCP (M. G. Hand) (10/26/84)

Well,  if you really want to know what the actual offset into your
data structure is (as opposed to some logical naming method like
struct.mem) you should try using BCPL instead of C.  Try this:

	LAST.LIST!NEXT := NEWVEC(SIZE.OF.LIST)
	LIST.MEM := LAST.LIST!NEXT
	PACKSTRING(LIST.MEM!NAME, "a string")

where NEXT and NAME are simple integers giving offsets into the vector
returned by NEWVEC.   Ie you get a simple contiguous block of BCPL words
and its is up to you how you partition it.  To get machine portability
you are guaranteed the manifets constants BCPLBYTESPERWORD and BITSPERBYTE
which can be used as scaling factors.   Fun, eh?

		marcus