• Coding
  • C challenge: know your pointers

I came across that exercise today, I thought I'd share. I won't give the source link for now so that you're not tempted to look at the answers. I could share it later. Also try not to use a computer while doing this, that would be too easy :P

Take this piece of code:
#include <stdio.h>

int main() {
  int x[5];
  printf("%p\n", x);
  printf("%p\n", x+1);
  printf("%p\n", &x);
  printf("%p\n", &x+1);
  return 0;
}
Assume we're working on a 32-bit machine (where sizeof (int)is equal to 4) and x is stored at address 0xffbfd7d8.

The question is simple: What will be the output of each call to printf?
I'm a bit rusty, but here it goes:
00ffbfd7d8
00ffbfd7dc
00ffbfd7d8
00ffdfd7ec
@mesa: It's correct.
I was hoping for a higher participation, as the discussion underlying the exercise is pretty interesting. Seems it's just you and me :)
Got the same answers as well.

The last one is the only real trick but the reasoning lies in determining what exactly is &x + 1.

Neat ;)