** p : how??

1
2
3
4
5
6
7
8
9
10
11
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
 static int a[]={0,1,2,3,4};
 static int *p[]={a,a+2,a+1,a+4,a+3};
 int **ptr;
 ptr=p;
 **++ptr;
 cout<<**ptr<<" "<<ptr-p<<" "<<*ptr-a;
} 


// plz. get me how the output comes?????
//ans: 2 1 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
a[0] = 0 // This is what happened when you set a
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 4

p[0] = a[0] = 0 // this is what happened when you set p
p[1] = a[2] = 2
p[2] = a[1] = 1
p[3] = a[4] = 4
p[4] = a[3] = 3

**ptr = p[0] = a[0] = 0 // this is what happened when you set ptr = p
**ptr = p[1] = a[2] - 2 // this is what happened after ++ptr

cout << **ptr    = p[1]       = a[2]    = 2
     << ptr - p  = p+1 - p    = 1
     << *ptr - a = *(p+1) - a = a+2 - a = 2
Last edited on
how this
<< ptr - p = p+1 - p = 1
when
1
2
 ptr=p    //ptr=p[0]=a[0]=0
 
@stewbond
how??
ptr=p+1;
if **ptr=0 ; ie.a[2]-2
ptr=p+1 comes from line 9: **++ptr;

Believe it or not, the ** doesn't do much in this line. It dereferences ptr, but it isn't used as there is nothing else in this line. Therefore, you can just read this as ++ptr which increments ptr from ptr = p to ptr = p+1.
Last edited on
Topic archived. No new replies allowed.