Pergunta de entrevista da empresa Apple

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

Respostas da entrevista

Sigiloso

10 de out. de 2009

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

4

Sigiloso

20 de mai. de 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.

Sigiloso

10 de out. de 2009

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