array of pointers as function parameter

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 decay 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 are always going to pass arrays of strictly 4 elements, it might be a better idea to use a differently-typed pointer as a parameter:

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

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

Leave a Comment