Say I have some code where I have a variable a and I allocate a pointer to point to a
:
int a = 5;
int* ptr = &a;
However, after this, I want to give the memory location that variable a
occupies the name b
.
int b = a; // doesn't work, because it only copies the value
&b = ptr; // doesn't work, not an assignable value
ptr = &b; // doesn't work, this moves the pointer to point to b
// instead of renaming the location that ptr already pointed to
Is this possible? (There's no good reason for doing this - just curious.)
-- Edit: This question is not about the differences between pointers and references, but rather how to achieve a goal through using them, and thus is not a duplicate of "what is the difference between references and pointers?"