Explain Pointer expression and Pointer Rule in C

 Explain Pointer expression and Pointer Rule in C

Like any other variable pointer variables can be used in expressions.
For example : y = *p1  *  *p2;
                        sum = sum + *p1;
                        z = 5* -*p2/ *p1;
                        *p2 = *p2 + 10;
are valid expressions.
C allows to add integers or to subtract integers from pointers as well as to subtract one pointer fro another.
For example : p1+4, p2-2, p1-p2 are valid.
We may also use short-hand operators with the pointers.
For example : p1++, -p2, sum+=*p2                          
 In addition to arithmetic operations pointers can also be compared using
  the relational operators.
  For example : p1 > p2, p1 = = p2, p1 ! = p2
 We may not use pointers in division or multiplication.
  For example : p1 / p2, p1 * p2, p1 / 3
  are not allowed.
 Two pointers cannot be added.
  For example : p1 + p2.
  

Rules of pointer operations


A pointer variable can be assigned the address of another variable.
A pointer variable can be assigned the values of another pointer variable.
A pointer variable can be initialized with NULL or zero value.
A pointer variable can be pre-fixed or post-fixed with increment or decrement operators.
An integer value may be added or subtracted from a pointer variable.
When two pointers point to the same array, one pointer variable can be subtracted from another.
When two pointer points to the objects of the same data type, they can be compared using relational operators.
A pointer variable cannot be multiplied by a constant.
Two pointer variables cannot be added.
A value cannot be assigned to an arbitrary address i. e. &x=10 is illegal.

Labels: