试图将地址传递给指针的引用

Trying to pass adress to a reffernce to a pointer

本文关键字:指针 引用 址传 地址      更新时间:2023-10-16

为什么调用funk(&a)会给出编译错误

func(int * & data) {data++;}
int main(){
int a = 5;
int *p = &a;
func(&a); //this gives a compile error
funk(p);  //this works fine 
}
错误:从类型为"int

*"的右值初始化类型为"int*&"的非常量引用无效

正如错误消息所解释的那样,参数需要引用一个变量,&a不是变量,而是变量的地址