Saturday, January 7, 2012

Pointer Arithmemtic

Which arithmetic operations are allowed in Pointer arithmemtic and why ?

Concept:
You can perform a limited number of arithmetic operations on pointers. These operations are:
  • Increment and decrement
  • Addition and subtraction
  • Comparison
  • Assignment
The increment (++) operator increases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.
The decrement (--) operator decreases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the -- makes the pointer refer to the first element in the array.
You can add an integer to a pointer but you cannot add a pointer to a pointer.
If the pointer p points to the first element in an array, the following expression causes the pointer to point to the third element in the same array:
p = p + 2;
If you have two pointers that point to the same array, you can subtract one pointer from the other. This operation yields the number of elements in the array that separate the two addresses that the pointers refer to.
You can compare two pointers with the following operators: ==, !=, <, >, <=, and >=.
Pointer comparisons are defined only when the pointers point to elements of the same array. Pointer comparisons using the == and != operators can be performed even when the pointers point to elements of different arrays.
You can assign to a pointer the address of a data object, the value of another compatible pointer or the NULL pointer.

No comments:

Post a Comment