when i am subtracting
int lb= lower_bound(arr,arr+n,6)-arr;
how can I store this value in int since both those value are address this should be stored in pointer how it is giving intiger value?
code-- https://ide.codingblocks.com/s/229357
How pointer value is stored in int
hello @anubhavb11
Pointer arithmetic in C is defined as follows-
Pointer arithmetic doesn’t allow following operations-
- Addition of 2 pointers
- Multiplication of a pointer with a constant(Integer)
- Division of a pointer with a constant
Pointer arithmetic allows following operations-
- Addition of a constant(Integer) to a pointer
- Subtraction of a constant(Integer) from a pointer
- Subtraction of 2 pointers
Now let’s see what happens in the above 3 operations-
Addition of a constant N to a pointer returns another pointer which holds the address of the element present after N blocks from current memory location in the memory.
Similarly Subtraction of a constant N from a pointer returns another pointer which holds the address of the element present before N blocks in the memory.
Subtraction of 2 pointers returns the number of blocks present between the 2 addresses pointed by the 2 pointers.(which is an integer of course…).