我的印象是对取消引用指针的引用地址与指针的地址相同(所以在这里提问)。
但是当我编写一个返回取消引用指针引用的函数时,我得到的地址与原始地址不同:
#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);
}
我从中得到的输出是:
second:10 third?-16121856
second:10 third: 15
Buff 5 addr:0x8050b80 reffy: 0xbf7089b0
该函数是创建一个临时变量并返回它的地址还是什么?我不太明白它为什么会这样做。