You're getting those errors because
reverse is supposed to return an
int
, but you're actually returning (and assigning to) an
int*
variable.
It seems to me that you have two options:
1) Have
reverse take (as parameters) an already allocated array, the original array, and the size, store the reverse of the original array into the other array, and return nothing (
void
).
Then
reverse might look something like
1 2 3 4 5 6
|
void reverse(int* dest, const int* src, int size)
{
// Assume that sufficient memory has already been allocated for 'dest'
for (int i = 0; i < size; ++i)
dest[i] = array[size-i-1];
}
|
2) Have
reverse allocate the memory for the reversed array and just return that.
In that case, you shouldn't be allocating any memory for
revArray in
main:
1 2 3 4 5 6 7
|
int* reverse(const int* array, int size)
{
int* revArray = new int[size];
for (int i = 0; i < size; ++i)
revArray[i] = array[size-i-1];
return revArray;
}
|
My gut feeling tells me to prefer the first, but I guess it's ultimately your choice....