I have a basic question on array and pointer in C/C++.
Say I have:
Foo* fooPtrArray[4];
How to pass the fooPtrArray
into a function?
I have tried:
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)
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?
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);
}
A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().
Method 1: Using the apply() method: The apply() method is used to call a function with the given arguments as an array or array-like object. It contains two parameters. The this value provides a call to the function and the arguments array contains the array of arguments to be passed.
Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.
Array of pointers: “Array of pointers” is an array of the pointer variables. It is also known as pointer arrays. Syntax: int *var_name[array_size];
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With