May 212011

For years, I’ve tortured DigiPen grads and intern candidates alike with this whiteboard interview question. Frankly, I’m sick of it and I need to switch things up. I’d expect senior engineers to get it fairly quickly, and on-the-metal types should just know this by rote. I wanted to see if the junior engineers could think through the problem logically, be circumspect in their reasoning, and if they were comfortable with pointers and basic compiler concepts. So here goes…

Given a simple C-structure:

struct foo_t
    int a;
    float b;
    char c[3];
    void * zPtr;

I would like you to write some code that outputs the number of bytes from the top of the structure to the beginning of the zPtr structure member. Your answer should be portable, fast, and resilient to code changes.

Aaaand Go! When you think you have it, the answer is over here.