C++ 指针数组作为函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1719051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:00:03  来源:igfitidea点击:

array of pointers as function parameter

c++arraysfunctionpointers

提问by Lily

I have a basic question on array and pointer in C/C++.

我有一个关于 C/C++ 中数组和指针的基本问题。

Say I have:

说我有:

Foo* fooPtrArray[4];

How to pass the fooPtrArrayinto a function? I have tried:

如何将 in 传递fooPtrArray给函数?我试过了:

int getResult(Foo** fooPtrArray){}  //  failed
int getResult(Foo* fooPtrArray[]){} // failed

How can I deal with pointer array?

我该如何处理指针数组?

EDIT:I once thought the error msg is from passing the wrong pointer array, but from all the responses, I realize that it's something else... (pointer assignment)

编辑:我曾经认为错误 msg 是由于传递了错误的指针数组,但是从所有响应中,我意识到它是别的东西......(指针分配)

Error msg:
Description Resource Path Location Type incompatible types in assignment of 
`Foo**' to `Foo*[4]' tryPointers.cpp tryPointers line 21 C/C++ Problem

I don't quite get why it says: Foo* * to Foo*[4]. If as function parameter they are inter-change with each other, why during assignment, it give me compilation error?

我不太明白为什么它说:Foo* * to Foo*[4]。如果作为函数参数它们是相互交换的,为什么在赋值过程中,它给我编译错误?

I tried to duplicate the error msg with minimum code as follows:

我尝试用最少的代码复制错误消息,如下所示:

#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);
}

回答by AnT

Both

两个都

int getResult(Foo** fooPtrArray)

and

int getResult(Foo* fooPtrArray[])

as well as

int getResult(Foo* fooPtrArray[4])

will work perfectly fine (they are all equivalent).

将工作得很好(它们都是等效的)。

It is not clear from your question what was the problem. What "failed"?

从您的问题中不清楚是什么问题。什么“失败”?

When passing arrays like that it normally makes sense to pass the element count as well, since the trick with allowing the array type to declay to pointer type is normally used specifically to allow passing arrays of different sizes

当传递这样的数组时,通常也传递元素计数是有意义的,因为允许数组类型延迟为指针类型的技巧通常专门用于允许传递不同大小的数组

int getResult(Foo* fooPtrArray[], unsigned n);
...
Foo* array3[3];
Foo* array5[5];
getResult(array3, 3);
getResult(array5, 5);

But if you always going to pass arrays of strictly 4 elements, it might be a better idea to use a differently-typed pointer as a parameter

但是如果你总是要传递严格的 4 个元素的数组,那么使用不同类型的指针作为参数可能是一个更好的主意

int getResult(Foo* (*fooPtrArray)[4])

In the latter case the function call will loook as follows

在后一种情况下,函数调用将如下所示

Foo* array[4];
getResult(&array);

(note the &operator applied to the array object).

(注意&应用于数组对象的运算符)。

And, finally, since this question is tagged as C++, in the latter case a reference can also be used instead of a pointer

最后,由于这个问题被标记为 C++,在后一种情况下,也可以使用引用而不是指针

int getResult(Foo* (&fooPtrArray)[4]);
...
Foo* array[4];
getResult(array);

回答by LeopardSkinPillBoxHat

What you have declared with the following line:

您使用以下行声明的内容:

Foo* fooPtrArray[4];

is an array of pointers to Fooobjects (in other words an array of Foo*).

是一个指向Foo对象的指针数组(换句话说,一个 的数组Foo*)。

In C/C++, the name of the array is defined as a pointer to the start of the array. This is because arrays aren't "real" types in these languages but simply a contiguous sequence of values of a specific type in memory.

在 C/C++ 中,数组的名称被定义为指向数组开头的指针。这是因为数组在这些语言中不是“真正的”类型,而只是内存中特定类型值的连续序列。

The name fooPtrArrayin this case will be a "pointer to the first pointer" in the array, which is the start of the memory location where the array of pointers is stored.

fooPtrArray在这种情况下,名称将是数组中的“指向第一个指针的指针”,它是存储指针数组的内存位置的开始。

So either function prototype you have outlined above should work. However, when passing in arrays to a function, you will always need to pass in the size as well so the function knows how many elements are in there. So you should define your method like this:

因此,您在上面概述的任何一个函数原型都应该可以工作。但是,当将数组传递给函数时,您总是需要传递大小,以便函数知道其中有多少元素。所以你应该像这样定义你的方法:

int getResult(Foo** fooPtrArray, int arraySize);

Inside this function, you can access the individual Foopointers (and subsequently Fooobjects) in the array like this:

在此函数中,您可以像这样访问数组中的各个Foo指针(以及随后的Foo对象):

for (int i=0; i < arraySize; i++)
{
    Foo* fooPtr = fooPtrArray[i];
    if (fooPtr)
    {
        fooPtr->memberFunction();
        fooPtr->memberVariable;
    }
}

回答by LeopardSkinPillBoxHat

The top one compiles OK:

上面的编译OK:

http://codepad.org/KOcnpmtv

http://codepad.org/KOcnpmtv

The second one is OK too:

第二个也可以:

http://codepad.org/7OSqprYI

http://codepad.org/7OSqprYI

回答by Julio

I don't see how this can compile without an int returned ?!

我不明白在没有返回 int 的情况下如何编译?!

@Kinopiko your code does not compile in C++.

@Kinopiko 你的代码不能用 C++ 编译。

@Lily what is the error message you get? in your example getResult needs to return an int. It's probably what is failing.

@Lily 你得到的错误信息是什么?在您的示例中,getResult 需要返回一个 int。这可能是失败的原因。

回答by Carl Norum

Both of those function signatures look like they should work. When calling the function, you would do something like this:

这两个函数签名看起来都应该可以工作。调用该函数时,您将执行以下操作:

int returnValue;
returnValue = getResult(fooPtrArray);