use*_*514 0 c++ pointers casting
不知道为什么我收到这个错误.我有以下内容:
int* arr = new int[25];
int* foo(){
int* i;
cout << "Enter an integer:";
cin >> *i;
return i;
}
void test(int** myInt){
*myInt = foo();
}
This call here is where I get the error:
test(arr[0]); //here i get invalid conversion from int to int**
Run Code Online (Sandbox Code Playgroud)
你编写它的方式,test
指向一个指向一个指针int
,但arr[0]
只是一个int
.
但是,foo
您正在提示输入int
,但读入的位置是未初始化指针的值.我原本以为你想foo
阅读并返回int
.
例如
int foo() {
int i;
cout << "Enter an integer:";
cin >> i;
return i;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,测试采用指向int(即void test(int* myInt)
)的指针是有意义的.
然后你可以传递一个指向int
你动态分配的指针.
test(&arr[0]);
Run Code Online (Sandbox Code Playgroud)