De-referencing Pointers : De-reference Operator (*)

From WikiEducator
Jump to: navigation, search

The de-reference operator ( * )  :

One of the most confusing operator in pointer notation is the use of the operator ( * ) asterisk. The meaning of the symbol (*) changes at following different situations:
i) when we declare a pointer variable
ii)when we try to read a value present at the address which is held by a pointer variable.

Let us understand the above two scenarios :
i) when we declare the pointer variable we are in habit to write :

  int * p ; // [ here the * only indicates that p is a pointer variable] 

ii) When we read a value from an address:

PointerPic3.JPG

PointerPic4.JPG

Execute the following and see which of the two lines are giving same output:
main( )
{

 int a , *ptr ;
a = 30 ;
ptr = &a;
cout<<a << "\n";
cout<<&a << "\n";
cout<<*ptr<<"\n";
cout<<ptr<<"\n";
getch( );

}