C/C++ Structure offset C/C++ Structure offset c c

C/C++ Structure offset


How about the standard offsetof() macro (in stddef.h)?

Edit: for people who might not have the offsetof() macro available for some reason, you can get the effect using something like:

#define OFFSETOF(type, field)    ((unsigned long) &(((type *) 0)->field))


Right, use the offsetof macro, which (at least with GNU CC) is available to both C and C++ code:

offsetof(struct mstct, myfield2)


printf("offset: %d\n", &((mstct*)0)->myfield2);