Thursday 7 May 2015

LEARN POINTERS

POINTER- A POINTER IS A VARIABLE WHICH HOLDS A MEMORY ADDRESS OF A ANOTHER VARIABLE OF SAME DATA TYPE.

BELOW PROGRAM WILL BE HELP FULL IN UNDERSTANDING POINTERS AND ALSO HELPS IN FINDING OUTPUTS.

int *iptr ;
int a= 25 ;
 iptr = &a ; or iptr =a ;   ----> here iptr holds and show(in output) memory address of 'a'

*iptr = a;                      ----> here itpr holds the value and show(in output) value (25) of 'a'

int b[10];
int *p;
p=b or p=& b[0]  -----> p hold the address of b

for(int i=0; i<2 ; i++)
{cout<< *p <<" ," << p;
p++ ;------->> after this statement p hold next address
}
assume address of b[0]= 1001 & value 25 and b[1]= 1002 & value 12
output- 
           25 , 1001
           12 , 1002

in int
initially pointer p points at the address of b[0].
then after (p + 2)---> p holds the address of b[2].

char t[]='HIM';
char *w ;
w = & t[0];
for(int i=0; i<2 ; i++)
{
       output of  *(w++) is H
                                       I
 ---------------------------------------------------------------------------------------------
       output of  *(++w) is I
                                      M   
 ---------------------------------------------------------------------------------------------     
    output of  ++*w++  is I
                                     J
 ---------------------------------------------------------------------------------------------
       )

No comments:

Post a Comment