Pointers

 Note:  Must see this post before reading the program’s explaination.  It will clear your doubts.
here is its link: https://jasleen7956.wordpress.com/2012/11/12/array-vs-pointer-array/
  
PROGRAM:

#include<stdio.h>
char *c[]={“ENTNG”,”NST”,”AMAZI”,”FIRBE”};
char **cp[]={c+3,c+2,c+1,c};
char ***cpp=cp;
void main()
{
printf(“%s”,**++cpp);
printf(“%s”,*–*++cpp+3);
printf(“%s”,*cpp[-2]+3);
printf(“%s”,cpp[-1][-1]+1);
}

Output of above program is  AMAZINGBEST

Explaination:

char *c[]={“ENTNG”,”NST”,”AMAZI”,”FIRBE”};
//initialise an array of pointers.
As already explained is a Two dimentional array pointer, where  c  points to c[0], which is again an array that store string “ENTNG”
Here,    *c[]  can be written as **c
c[0] = ENTNG
c[1] = NST
c[2] = AMAZI
c[3] = FIRBE
 
 
char **cp[]={c+3,c+2,c+1,c};
//initialise array of pointer to pointers
cp is a pointer variable pointing to c[3]
cp[0] -> c+3     i.e  FIBRE
cp[1] -> c+2     i.e  AMAZI
cp[2] -> c+1     i.e  NST
cp[3] -> c          i.e  ENTNG
 
 
char ***cpp=cp;
pointer to (pointer to pointer)
can be written as **cpp[] ,  cpp points to first element of cp[0]
 
cpp[0] -> cp[0]        FIRBE
cpp[1] -> cp[1]        AMAZI
cpp[2] -> cp[2]        NST
cpp[3] -> cp[3]        ENTNG
 
 
 
printf(“%s”,**++cpp);
 
 BEFORE INCREMENT:  
cpp points to cp[0]
  _____________________
|_c+3_|_c+2_|_c+1_|__c_|          cp
  cp[0]    cp[1]    cp2]     cp[3]
 
 ______
|_c+3_|     cpp     // cpp->cp[0]
 
 

AFTER INCREMENT:

++cpp ,  cpp is incremented,  now it points to cp[1]

  ______________________
|_c+3_|_c+2_|_c+1_|__c__|          cp
  cp[0]   cp[1]    cp[2]   cp[3]
 
 ______
|_c+2_|         cpp   //cpp->cp[1]
 
 
++cpp increments cpp as cpp+1
i.e cpp = &cp[0]
 and  after ++cpp,    cpp- >&cp[1]
 
for instance we can say: 
cp[0]    FIBRE
// we can’t say cpp[-1] == cp[0]  because negative index cause undefined behavior 😉
cpp[0] -> cp[1]     AMAZI
cpp[1] -> cp[2]      NST
cpp[2] -> cp[3]      ENTNG
 
 
Now,
++cpp -> cp[1]       // cp[1] -> c+2
++cpp = &cp[1]       // &(c+2)
*++cpp = *(&c+2)     //  c[2]
**++cpp = *&c[2]     // which is AMAZI
 
 
printf(“%s”,*–*++cpp + 3);
 ++cpp ->cp[2]   //   now, pointer points to cp[2] instead of cp[1]
therefore,
 
for instance we can say:
cp[0]   FIBRE            //can’t say  here cpp[-2] ==cp[0]
cp[1]   AMAZI           //can’t say here cpp[-1] ==cp[1]
cpp[0] -> cp[2]     NST
cpp[1] -> cp[3]     ENTNG
 
 –*(++cpp)
cp[2] -> (c+1)   and   *cp[2] -> *(c+1)  
–*cp[2]=*&(c+1-1)   // c
 
*–*++cpp = *(&c[0])  // c[0]       ENTNG
 *–*++cpp = ENTNG        //c[0]
Therefore,
*–*++cpp  + 3  = c[0][3]   //row= 0  column =3   of pointer array  ‘c’
 i.e    NG
 
printf(“%s”,*cpp[-2]  + 3);
As   pointer  cpp -> cp[2]
cpp = &cp[2] 
*cpp = *&cp[2]    //  address of cp+2
*cpp[-2] = cp+2 – 2    // =  cp[0],   cp[0] is c+3
i.e    FIRBE
Now,
*cpp[-2]  = c[3][0]
*cpp[-2]  + 3  = c[3][3]        // row =3, column =3
i.e  BE
 
printf(“%s”,cpp[-1][-1]  + 1);
As   pointer  cpp -> cp[2]
cpp = &cp[2] 
cpp[-1] == &cp[2]) – 1  == (cp+2) -1  == cp[1] == c+2
cpp[-1][-1] == (c+2 ) -1 == c[1] == NST
Now,
cpp[-1][-1] +1  == ST
 
 
Hence its output is  AMAZINGBEST           🙂           🙂            🙂              🙂
 
 
 

About Jasleen Kaur

Hi, I am Jasleen kaur, a Computer Science Student.
This entry was posted in C++ and tagged , , . Bookmark the permalink.

5 Responses to Pointers

  1. >As pointer cpp -> cp[2]
    >cpp = &cp[2]
    >*cpp = *&cp[2] //cp+2

    Here *cpp = cp[2] Not cp+2

    Because cp+2 = &cp[2] by conventions.

  2. jasleen kaur says:

    *&cp[2] and cp[2] are same .

  3. Vigas deep says:

    Fantastic explanation, Pleasure to read it.

  4. albertcoder says:

    The link in the beginning is not working. I wished to read it. 😦

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s