Addition of a number to pointer: Pointer+ number
When we add some integer to the pointer then the address stored in the pointer is incremented by
number * sizeof (datatype of pointer)
Assume space required for data types int & char are 2 & 1 bytesresp.
And we represent a particular block of memory filled with random values (not shown) as:
If we declare the pointers as
int *x:
int *y:
int i=3;// starting address of i is 9 and is 2 byte wide.
x=&i;
printf(“value at x is %d”, *x); // output we get is 3 i.e. value stored at location 9 & 10
x=x+1; // now x points to space with starting address 11
printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 11 & 12
x=x+3; // we add 3*2=6 more i.e. now x points to address 17
printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 17 & 18
If we declare the pointers as
char *x:
char *y:
char i // starting address of i is 9 and is 1 byte wide.
x=&i;
printf(“value at x is %d”, *x); // output we get is the value stored at location 9
x=x+1; // we add 1*1=1 and now x points to space with starting address 10
printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 10
x=x+3; // we add 3*1=3 more i.e. now x points to address 13
