I am writing the following to pass pointers by reference. However, when I try to dereference the pointers, it is giving unexpected values.
void passPointers(int* &a){
int p = 5;
a = &p;
}
int main(){
int x = 3;
int *y= &x;
cout<<"y is "<<y<<" *y is "<<*y<<endl;
passPointers(y);
//cout<<"y is "<<y<<" *y is "<<*y<<endl;//line a
cout<<" *y is "<<*y<<endl;//It returns 5
return 0;
}
If I uncomment line a, it returns address of y , *y returns some unknown integer value.Am I breaking some norms of C++. I used this link while writing this code. I am using g++ 7.3.0