I have this maybe weird question. I was considering following example where I pass a pointer by reference so if I change the pointer address of the argument the initial pointer should also change the address. And indeed this happens.
However the value of a in the main method will have some random value and not 3 as the q variable inside changePointer
whose address we use to change pointer a to.
Is there something stupid oversee or is the whole thing just undefined behaviour.
#include <iostream>
void changePointer(int* &p) {
int q = 3;
std::cout << "Address of q: " << &q << std::endl;
p = &q;
std::cout << "Value of p: " << *p << std::endl;
}
int main() {
int* a = new int(4);
changePointer(a);
std::cout << "Adress of a: " << a << std::endl;
std::cout << "Value of a: " << *a << std::endl;
}