jeff_ang24
jeff_ang24

Reputation: 129

Pointer inside pointer : How to know easily between two pointer?

This is my simple exercise:

int a = 10;
int b = 5;
int *p = &a;
int **p2 = &p;
int *p3 = &b;
*p = **p2 + *p3;
*p3 = (**p2)--;
*p2 = p3;
**p2 = **p2 + 15;

The answer key said that the value of a is 14 and b is 30. How it can be? I'm very stuck at the pointer **p2, I need some explanation about this pointer.

Upvotes: 0

Views: 86

Answers (3)

Yogesh Tripathi
Yogesh Tripathi

Reputation: 735

:)

int a = 10;
int b = 5;
int *p = &a;
int **p2 = &p;
int *p3 = &b;
*p = **p2 + *p3;
*p3 = (**p2)--;
*p2 = p3;
**p2 = **p2 + 15;

Starting from the above -

1- 10 is assign to a.

2- 5 is assign to b.

3- address of a is assign to pointer variable p.

4- address of pointer variable 'p' is assign to p2(which is a pointer to pointer variable).

5- address of b is assign to pointer variable p3.

6- A we know * is used to dereference the pointer. So here in **p2(you can understand it like this *(*p2) ) first * will dereference p2 which will give address of a and then the second * operator will deference it and give value of a. So *(*p2) is the value of a. Now in the same expression in the *p3.p3 is holds the address of b so *p3 will give the value of b. The result what you will get is 15.

7- In this expression first p2 will be deference then decremented by 1.As you know a is now 15 therefore it will become 14 after decrement. And *p3 will be 15(Read this : http://msdn.microsoft.com/en-us/library/dy3d35h8.aspx).

8- Since **p2 is now 15 therefore 30 will be assigned into **p2. Hope my explanation cleared your doubts.

Upvotes: 0

John Bode
John Bode

Reputation: 123548

int a = 10;
int b = 5;
int *p = &a;
int **p2 = &p;
int *p3 = &b;

After these declarations and initializations, all of the following are true:

  p2 == &p
 *p2 ==  p  == &a
**p2 == *p  ==  a == 10
         p3 == &b
        *p3 ==  b ==  5

So we can substitute the pointer expressions for the things they point to:

*p = **p2 + *p3 ==> a = a + b // 10 + 5 == 15
*p3 = (**p2)--  ==> b = a--;  // b = 15, a = 14

Remember that x-- evaluates to the current value of x, and as a side effect decrements it by 1.
So, in b = a--, b gets the value of a before the decrement.

After the expression

*p2 = p3 // equivalent to p = p3; p now points to the same thing as p3

our table now looks like

  p2 == &p
 *p2 ==  p ==  p3 == &b
**p2 == *p == *p3 ==  b == 15

Leaving us with

**p2 = **p2 + 15 ==> b = b + 15

So when we're done, b is 30 and a is 14.

Upvotes: 0

jean-loup
jean-loup

Reputation: 609

int a = 10;
int b = 5;
int *p = &a;
int **p2 = &p;
int *p3 = &b;

a is 10, b is 5, p points at a, p2 points at p and p3 points at b.

*p = **p2 + *p3;

a's value (p points at a) is set to **p2 (what is pointed by p: a) plus *p3 (which points at b): 15

*p3 = (**p2)--;

b's value (p3 points at b) is set to **p2 (what is pointed by p: a): 15. And then a is decremented: 14.

At this point, a is 14 and b is 15.

*p2 = p3;

What p2 is pointing (p) is set to p3. *p2 will now point to what p3 is pointing: b

**p2 = **p2 + 15;

b's value is set to itself plus 15: 30

Upvotes: 3

Related Questions