Pointer Arithmetic
As you understood pointer is an address which is a numeric
value; therefore, you can perform arithmetic operations on
a pointer just as you can a numeric value. There are four
arithmetic operators that can be used on pointers: ++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is
an integer pointer which points to the address 1000. Assuming
32-bit integers, let us perform the following arithmatic
operation on the pointer:
ptr++
the ptr will point to the location 1004 because each time
ptr is incremented, it will point to the next integer.
This operation will move the pointer to next memory
location without impacting actual value at the memory location.
If ptr points to a character whose address is 1000, then above
operation will point to the location 1001 because next
character will be available at 1001.
No comments:
Post a Comment