我有一个测试函数,它接受一个数组作为参数。我有一个指针数组。有人能解释一下,为什么我需要在传递指针时将指针转换为指针数组吗?
void test(States a[]){
cout << a[0].name << endl;
cout << a[1].name << endl;
}
字符串
呼叫test()
:
States *pStates[MAX_STATES];
test(*pStates); //Or test(pStates[0])
//Can't do test(pStates);
型
3条答案
按热度按时间b1zrtrql1#
如果测试函数的参数期望如此,则不需要解引用
字符串
但是在你的例子中,很明显参数类型是
States []
,所以你需要传递一个指针。您可能需要考虑将测试函数重写为:
型
hpxqektj2#
用这个代替:
字符串
你不需要去引用它...
nnsrf1az3#
pStates
的声明声明了一个指针数组。不是指向数组的指针。但是函数void test(States a[]);
需要一个对象数组(States
对象)。你不能把一个推到另一个。
字符串