Subtraction of a number from the pointer : Pointer number
When we subtract some integer from the pointer then the address stored in the pointer is decremented 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=x1; // now x points to space with starting address 7
printf(“value at x is %d”, *x); //output we get is random value i.e. value stored at location 7& 8h
x=x3; // we subtact 3*2=6 more i.e. now x points to address 1
printf(“value at x is %d”, *x); // output we get is random value i.e. value stored at location 1&2
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=x1; // we subtract 1*1=1 and now x points to space with starting address 8
printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 10
x=x3; // we subtract 3*1=3 more i.e. now x points to address 7
