C++ 40: Pointer Arithmetic
- We can use only +,-,++,--,+= and - = operators with the pointer variable itself.
- Adding with pointer instruct the pointer to point to next value in the memory,
- Similarly, subtracting from the pointer will instruct the pointer to point the previous value in the memory.
That's why:
- if we add 1 with integer pointer then the address in the pointer will increase by 2, and if we subtract 1 from the pointer then the address pointer will decrease by 2. Because the next or previous value is 2 bytes away from its current location.
- If we add 1 with the float pointer then the address in the pointer will be increase by 4, and if we subtract 1 from float pointer will be decrease by 4 Because the next and previous value is 4 Bytes away from its current location.
- Same for remaining data types.
Note:
The size and capacity of data-types may change according to the 16 bit or 32 bit processes or, different Operating System and different compilers.
Example:
In 16-bit memory int store 2 bytes memory size.
In 32-bit memory int data type takes 4 bytes memory size.
Comments
Post a Comment