我有一个关于C/C++中数组和指针的基本问题。
假设我有:
Foo* fooPtrArray[4];
如何将
fooPtrArray
传递给函数?
我已经尝试过:int getResult(Foo** fooPtrArray){} // failed
int getResult(Foo* fooPtrArray[]){} // failed
我该如何处理指针数组?
编辑:我曾经认为出错信息是由于传递了错误的指针数组,但是从所有的回复中我意识到它是其他问题...(指针赋值)
Error msg:
Description Resource Path Location Type incompatible types in assignment of
`Foo**' to `Foo*[4]' tryPointers.cpp tryPointers line 21 C/C++ Problem
我不太理解为什么它说:Foo* * to Foo*[4]。如果作为函数参数它们可以互换,为什么在赋值时会导致编译错误?
我尝试用最少的代码复制错误消息,如下所示:
#include <iostream>
using namespace std;
struct Foo
{
int id;
};
void getResult(Foo** fooPtrArray)
{
cout << "I am in getResult" << endl;
Foo* fooPtrArray1[4];
fooPtrArray1 = fooPtrArray;
}
int main()
{
Foo* fooPtrArray[4];
getResult(fooPtrArray);
}