Apple Interview Question

Define a macro for byte offset of a given field in a structure.

Interview Answers

Anonymous

Oct 10, 2009

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

4

Anonymous

May 20, 2014

That is incorrect, &(type *)0 will not even compile, its an absolute (constant) value , it needs a memory location, and a valid one too. (int)& (((type*)0)->member) should suffice, C compiler will then take it as an address relative to start of structure and hence will give the offset.

Anonymous

Oct 10, 2009

-->args x - field y - structure #define GETBYTEOFFSET(x,y) { \ &(y) - &(y.x); \ }