I was under the impression that addresses of references to a dereferenced pointer were the same as the address of the pointer (so question here).
But when I write a function that returns a reference of a dereferenced pointer, I'm getting a different address from the original:
#include <iostream>
using namespace std;
int buff[10] = {0};
int& getSecond();
int main(){
buff[0] = 5;
buff[1] = 10;
buff[2] = 15;
int second = getSecond();
int* sp = (int*)&second;
cout << "second:" << *sp << " third?" << *(sp+1) << endl;
cout << "second:" << *(buff + 1) << " third: " << *(buff + 2) << endl;
cout << "Buff " << *buff << " addr:" << &(*buff) << " reffy: " << &second << endl;
}
int& getSecond(){
return *(buff + 1);
}
The output I'm getting from that is:
second:10 third?-16121856
second:10 third: 15
Buff 5 addr:0x8050b80 reffy: 0xbf7089b0
Is the function creating a temporary variable and returning its address or something? I can't quite figure out why it would be doing this.