#include<stdio.h>
int main(void){
int *ptr,a,b;
a = ptr;
b = ptr + 1;
printf("the vale of a,b is %x and %x respectively",a,b);
int c,d;
c = 0xff;
d = c + 1;
printf("the value of c d are %x and %x respectively",c,d);
return 0;
}
the out put value is
the vale of a,b is 57550c90 and 57550c94 respectively
the value of c d are ff and 100 respectively%
it turns out the ptr + 1 actually, why it behave this way?
Consider what a pointer is… it’s a memory address. Every byte in memory has an address. So, if you have an
intthat’s 4 bytes and its address is 1000, 1001 is actually the 2nd byte of thatintand 1002 is the third byte and 1003 is the fourth. Since the size of anintmight vary from compiler to compiler, it is imperative that when you increment your pointer you don’t get the address of some middle point in theint. So, the job of figuring out how many bytes to skip, based on your data type, is handled for you and you can just use whatever value you get and not worry about it.As Basile Starynkvitch points out, this amount will vary depending on the
sizeofproperty of the data member pointed to. It’s very easy to forget that even though addresses are sequential, the pointers of your objects need to take into account the actual memory space required to house those objects.