Pregunta de entrevista de Apple

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

Respuestas de entrevistas

Anónimo

10 oct 2009

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

4

Anónimo

20 may 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.

Anónimo

10 oct 2009

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