LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 October 18 2011

Joe
Member

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

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?

Offline

#2 October 18 2011

mesa177
Member

Re: C challenge: know your pointers

I'm a bit rusty, but here it goes:

00ffbfd7d8
00ffbfd7dc
00ffbfd7d8
00ffdfd7ec

Offline

#3 October 22 2011

Joe
Member

Re: C challenge: know your pointers

@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 :)

Offline

#4 October 22 2011

WizaRd
Member

Re: C challenge: know your pointers

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 ;)

Last edited by WizaRd (October 22 2011)

Offline

Board footer