Pointer Arithmetic
·
Pointer is a variable
that points to a memory location.
·
Memory addresses are
numeric value that ranges from zero to maximum memory size in bytes.
·
These addresses can
be manipulated like simple variables.
·
Increment, decrement,
calculate or compare these addresses manually can be done.
·
C language provides a
set of operators to perform arithmetic and comparison of memory addresses.
·
Pointer arithmetic
and comparison in C is supported by following operators -
Ø Increment and decrement ++ and --
Ø Addition and Subtraction + and -
Ø Comparison <, >, <=, >=, ==,
!=
Pointer
Increment and Decrement
·
Increment operator when used with a
pointer variable returns next address pointed by the pointer.
·
The next address returned is the sum
of current pointed address and size of pointer data type.
· For example, consider the below statements.
intnum=5;// Suppose address of num = 0x1230
int*ptr;// Pointer variable
ptr=#// ptr points to 0x1230 or ptr points to num
ptr++;// ptr now points to 0x1234, since integer size is 4 bytes
ptr--;// ptr now points to 0x1230