Consider the following code -
#include <iostream>
#include <stdio.h>
const int & retRef() {
return 6;
}
int main()
{
const int& k = retRef();
printf("Value: %d\n", k);
printf("Address: %p\n", &k);
printf("Value: %d\n", k);
return 0;
}
The output is -
Value: 6
Address: 0x7ffd45bf544c
Value: 32692
Why the value changed after printing the address of the variable k
? If I replace the line const int& k = retRef()
with const int& k = 6;
, the output is as expected.
Why is this different behavior? Thanks in advance