NVIDIA Interview Question
Software Engineer / Developersdo you this is a question for dummies ?
abc is not an *object* here
it should be:
#define offset (abc, c) (char *)(&((abc *)0->c))
I think your solution may be incomplete...
it seems to me should be:
#define offset(abc,c) ((char*)&(((abc*)0)->c)-(char*)(abc*)0)
#define offsetof(s,m) (size_t)&(((s *)0)->m) - (size_t)&(((s *)0))
((s *)0) takes the integer zero and casts it as a pointer to s.
((s *)0)->m dereferences that pointer to point to structure member m.
&(((s *)0)->m) computes the address of m.
(size_t)&(((s *)0)->m) casts the result to an appropriate data type.
By definition, the structure itself resides at address 0. It follows that the address of the field pointed to (Step 3 above) must be the offset, in bytes, from the start of the structure. At this point, we can make several observation
- dan January 17, 2011