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:
  1. 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.
  2. 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.
  3. 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

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected