Home > other >  Modify Pointer Value
Modify Pointer Value

Time:11-05

Really dumb question, but I am very confused.

So, my understanding is that if you have:

int i(3);
int& j(i);
int* k(&i);

In this case, if the value of j changes, then the value of i will also change, because j is basically an alias of i? On the other hand, if we make k point to something else, then the value of i and j will not change at all, right?

If the above is true, then if we have:

a = 1; b = 2;
p = &a; q = &b;
*p = *q;

Then why the value of a changes to 2? I thought p merely points to something else and that does not modify the value of a?

CodePudding user response:

Conjecture 1 is correct.

So long as k is pointing at i (and by the way int* k(&j); will also point k at i because j IS i) *k=10; will set i to 10. If you point k somewhere else then *k=10; will change whatever it now point at.

But in the second part, nothing ever changes p to point away from a, so writing to *p will change a. q is entirely separate from p, so pointing q at b simply means q points at b and *p = *q; is effectively a=b Change where either of those pointers points and you'll get a different result.

CodePudding user response:

In your first example, i is a variable, with a number of properties - two of those are an address in memory and a name (i). j is an alias for i, so is simply an alternative name for the same memory location as i. Or, to put it another way, the underlying memory location has two names - one name is i and the other is j.

Any operations that affect the underlying memory simultaneously change the values of both i and j. So assigning to i affects (via the memory location) the value of j, and vice versa. For example, i = 42 will work by changing the content of the underlying memory location and, since j is an alternative name for the same memory location, will cause the condition j == 42 to be true, and vice versa.

k is a variable which contains the address of i (i.e. the address of the underlying memory location). Assigning to *k affects the memory location, so changes i and therefore j. For example, *k = 21 will cause i == 21 && j == 21 to be true.

One key difference between a pointer and a reference is that a pointer can be reassigned (so it contains the address of a different memory location) but a reference cannot. So it is not possible to associate either i or j with a different memory location. Whereas, the assignment k = &some_other_int will cause k to contain the address of some_other_int - and k will no longer have any association with i or j. Doing anything with *k will affect the memory location that has the name some_other_int, but not the values associated with i or j.

In your second example, the value of p is the address of a and the value of q is the address of b. However, the expression *p = *q works by extracting the value at the memory location pointed to by q (i.e. the value of b) and assigning it into the memory location pointed to by p (which has the name a). So *p = *q has the same net effect as *p = b or a = b or even a = *b. In this sense, *p and *q can be viewed as references to (or alternative names or aliases of) a and b respectively.

  • Related