offsets in structures.

john at orion.UUCP john at orion.UUCP
Sat Oct 6 06:44:32 AEST 1984


[]
Here's a little *trick* that I picked up today to calculate offsets
of elements in structures.

#define OFFSET(X, Y)  ((struct X *)0)->Y


e.g.
	offset for an integer field  --  &OFFSET(mystruct, anyint)
	offset for a character array --  OFFSET(mystruct, arrayname)

Although there are more readable ways to write this using +/- and & I reckon
its kinda neat.

Also from the same source....
e.g. to rename files with suffix .n without suffix

	for i in *.n
	do
		ifs=$IFS; IFS=.; set $i; IFS=$ifs
		cp $i $1
	done

orion!john



More information about the Comp.lang.c mailing list