Pointers and their Rules in C

Pointers and their Rules in C Language

Arithmetic operation on pointers

 

int i1,i2;

in t*p1,*p2;

p1=&p1;

p2=&p2;

 

  1. p1+p2; error
  2. p1+1, Next address
  3. ++p1,p1++ = Next Address
  4. p2-p1 No of elements
  5. p2-1; previous elements address
  6. - -p2, Previous Address
  7. p1*p2; error
  8. p1*5; error
  9. p1/p2; error
  10. p1/p2; error
  11. p1%p2; error
  12.  p1%2.error p2- -

 

 

Pointer rules :-

Rule 1:

Address + Number = Address (Next Address)

Address – Number  = Address(pre Address)

Address ++=Address(Next address)

Address--=Address(Pre Address)

++ Address= Address(Next Address)

--Address = Address(Pre address)

 

Rule  2:-

Address – Address = Number(No.of elements)

= size diff/sizeof(datatype)

int*p1=(int*)100;

int*p2=(int*)200;

p2-p1=50

200-100 ------- > 100/size of(int)

 

Rule 3:

Address + Address=Illegal.

Address * Address= illegal

Address/Address= illegal

Address%Address=illegal

 

Rule 4:

We can use relational operator and conditional operator b/w two pointers(<,>,<=,>=,==,!=,?!)

Address  > Address = T/F

Address  > = Address = T/F

 

Rule 5:      

We can’t perform bitwise operation b/w two pointers like

Address & Address = illegal

Address | Address=Illegal

Address ^ Address = illegal

~ Address=illegal

 

Rule 6:

We can find size of a pointer using size of operator.