0

I have an array declared as

Object array[N];

and a function as

void GetArray(void** array, size_t count)
{
    *array = 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]
2
  • What do you want to do? Copy the values of the array to another array or point the 2 arrays to the same data? Commented Apr 7, 2011 at 17:34
  • point the two arrays to the same data Commented Apr 7, 2011 at 17:37

3 Answers 3

2

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
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@ise I edited my question with the actual code, it compiles fine but when I compile I have a warning about unitialized variable and the program stop working with a runtime error
@ise It gives me an error at runtime: arr is unitialized. I edited my question with a thing that could be important
@Stefano: Strange... I cannot reproduce the problem you mentioned. Here is a test on ideone.
@ise sorry my fault, I forgot the & after Object* in the function params
0

Just as with number, you should pass the address of array:

GetArray(&array, &number)

But with C++, you're better off using reference parameters.

1 Comment

ok but I didn't understand how I should declare the array variable to pass to the function. Could you explain me?
0

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 "&"

3 Comments

ok but I have a problem: count is the size of the array becuase after calling the GetArray function I need to cycle the array. Now, array how should be declared?
I mean use this code to loop the array: for (int i = 0; i < (int)number; i++)
yes I know how to loop the array but I don't understand how I should declare the "array" variable. To declare an array I need the dimension and the dimension is count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.