1

I have this question and answer as provided below:

int m = 5; int n = m; int *p1 = &m; int *p2 = &n; m = 10; p2 = p1; *p2 = 7; 

The question asks what are the value of all the following and the answers are:

m=7, n=5, *p1=7, *p2=7

This is what I tried:

m is an int of value 5. n is an int whose value is assigned to the value of m, which is 5 and p1 and p2 and initialised to be pointers to m and n.

Then I change the value of m to be 10, so m is 10 and n is 5

Then when I set p2 = p1, both pointers point to m

When I change the variable pointed to by p2 to be 7, n becomes 7, so m = 10, n = 7 and p1 and p2 points to n which has a value of 7.

Therefore:

m=10, n=5, *p1=7, *p2=7

what am I doing wrong here?

EDIT: SOLVED

5 Answers 5

4

Your error is on the last line of you answer. p2 is pointing to the memory address of m, not n.

p2 = p1 is that the memory address stored on p2 will be the address store in p1 that is the memory address of m.

n never change. *p2 = 7; happen after p2 = p1, when p2 is the memory address of m.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I think it makes sense now. When I set p2 = p1, both pointers point to m, so when I change the variable pointed to by p2 to be 7, m becomes 7 while n is unaffected, so m = 7, n = 5 and p1 and p2 points to n which has a value of 7. Thanks!
yes that is good. Please set the answer as correct on the mark on the botton of the votes.
2

Of the variables, say "x is ...", of the pointers say, "... points to x", and when dereferencing say "(the thing x points to) ...". Then the sequence of operations can be said to be:

  • m is 5
  • n is 5
  • p1 points to m
  • p2 points to n
  • m is 10
  • p2 points to m (because p2 gets the value of p1)
  • ( the thing p2 points to [m] ) is 7

Now look at the answers you have and see if it makes sense.

Comments

2

p1 and p2 both point to m. Therefore, the value of *p1 and *p2 is the value of m, which is 7. If you change the value of the variable your pointer is pointing to, the pointer will still point to it.

5 Comments

Hmm, ok, what about m? It was changed to 10, why did it become 7?
@TeoZec: don't you mean m instead of n?
Both m and n don't have the right value, basically.
@mouviciel yes, of course. I mistyped it. I'm really sorry. I'll correct it
@user3717271 I'm sorry, there was an error in my answer. Try reading it now.
1

You say: When I change the variable pointed to by p2 to be 7, n becomes 7

But you also say, just before: Then when I set p2 = p1, both pointers point to m

I see a contradiction here.

1 Comment

I was just editing it when this was posted. I think I have fixed it. Sorry for that.
1

Ok I hope this might shed some light

int *p1 = &m; int *p2 = &n; m = 10; p2 = p1; *p2 = 7; int m = 5; +-----+ m | 5 | +-----+ int n = m; // copy value, m and n are in two diff places +-----+ n | 5 | +-----+ int *p1 = &m; // set pointer to m +-----+ p1 -> m | 5 | +-----+ int *p2 = &n; // set pointer to n +-----+ p1 -> m | 5 | +-----+ +-----+ p2 -> n | 5 | +-----+ m = 10 +-----+ p1 -> m | 10 | +-----+ +-----+ p2 -> n | 5 | +-----+ p2 = p1 +-----+ p1 -> m | 10 | +-----+ / p2 --+ *p2 = 7 +-----+ p1 -> m | 7 | +-----+ / p2 --+ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.