Get an array via function-param
I have an array declared as
Object array[N];
and a function as
void GetArray(void** array, size_t count)
{
*array =开发者_StackOverflow array;
*count = N;
}
I'm trying to call the function with this code:
size_t number;
GetArray(XXX, &number);
where is XXX what should I pass to get the array? Thank you
EDIT 1
Object *array
GetArray((void**)array, number)
EDIT 2
static Object array[N]
Though I'm not 100% convinced that I understand your intent correctly,
if GetArray
has to return Object array[N]
itself, how about returning
Object*
from GetArray
?
For example:
size_t const N = 1;
Object array[N];
Object* GetArray(size_t* count)
{
*count = N;
return array;
}
EDIT:
As far as I see your edited question, the argument number
for GetArray
seems to be taken as a reference(not pointer).
So, as for the array too, how about taking a reference instead of a pointer?
Then you can avoid the troublesome void**
stuff.
For example:
void GetArray(Object*& arr, size_t& count)
{
arr = array;
count = N;
}
int main() {
size_t number;
Object *arr;
GetArray(arr, number);
for ( size_t i = 0; i < number; ++ i ) {
Object o = arr[i]; // example
}
}
Just as with number, you should pass the address of array:
GetArray(&array, &number)
But with C++, you're better off using reference parameters.
The array variable is a pointer to the first element of the array (its value is the address of the first element).
void GetArray(Object* dest, size_t* count)
{
dest = source; // source's value is an address; just copy it to dest
*count = N;
}
...
GetArray(array, &number); // array's value is an address so you don't need "&"
精彩评论