0
为什么我无法通过函数初始化具有预定义指针变量的引用变量?使用函数将参考变量初始化为指针
int * f(int * p) {
return p;
}
int main(){
int s=10;
int *p=&s;
int *&ref1 = p; //no error given here
int *&ref2 = f(p); //gives error cannot initialize int * & with int * why??
}
你确定你想提出的“修复”而不看到'f'的实现吗?你可能只是通过创建编译器没有捕获的悬挂引用来创建下一个更糟糕的问题。 –
@DanielFrey我假设他正在返回'main'内创建的指针。 – 0x499602D2
你将如何获得该指针?注意'f'的参数是该指针的一个*副本,所以你不能返回一个引用! –