How to get a byte offset

Peter Klausler pmk at craycos.com
Tue May 29 02:35:15 AEST 1990


>Is there an easy way to get the byte offset of a field in a structure without
>counting it manually ?

In ANS C, #include <stddef.h> and use the offsetof macro.

If you don't have access to a standard compiler, try this common trick:

#define offsetof(type,member)	((char *) &((type *) 0)->member - (char *) 0)

-Peter Klausler, a compiler guy at Cray Computer Corp. in Colorado Springs



More information about the Comp.lang.c mailing list