Pointer arithmetic for structs

user950891 picture user950891 · Oct 2, 2011 · Viewed 26.1k times · Source

Given a struct definition that contains one double and three int variables (4 variables in all), if p is a pointer to this struct with a value 0x1000, what value does p++ have?

This is not a homework problem, so don't worry. I'm just trying to prepare for a test and I can't figure out this practice problem. Thanks

This is in C. Yes I want the value of p after it is incremented. This is a 32-bit machine

Answer

tangrs picture tangrs · Oct 2, 2011
struct foobar *p;
p = 0x1000; 
p++;

is the same as

struct foobar *p;
p = 0x1000 + sizeof(struct foobar);