3

I have 4 sorted integer arrays, which i'm trying to merge into one huge sorted array.

I merge A and B together which gives me another int array called X Then I merge C and D together which gives me another int array called Y Finally i merge X and Y together to get Z, which is the final product.

The merge function is doing exactly the same each time, just storing the results into a different array which i want to pass in by reference.

I want to do something like this:

void mergeSort(int arr1[], int arr2, int &result[]){
    ...
}

But i get the error "Array of reference is not allowed". What is the best way to do this?

user1636130
  • 1,615
  • 5
  • 29
  • 47
  • Can you use any of the solutions provided in this post? https://stackoverflow.com/questions/12791266/c-concatenate-two-int-arrays-into-one-larger-array – Cory Kramer Mar 10 '14 at 11:57
  • 3
    Why are you not using `std::vector`s? – jrok Mar 10 '14 at 11:59
  • 1
    @Michał Góral: `std::array` and only in C++11... – hivert Mar 10 '14 at 12:08
  • array names are actually pointers, so you dont have to pass them by reference (you can say that arrays are passed by reference, by default), just as you passed int arr1[], pass result array that way – Ali Kazmi Mar 10 '14 at 12:09
  • It's impossible if you really really don't know the array's size (i.e. it only can be determined by user on runtime). But you can do it if you know the size though it's variant by using template. – Hzzkygcs Mar 27 '19 at 11:56

4 Answers4

7

The syntax to pass an array by reference in C++ is

int (&result)[size]

note that you need to know the size at compile time. This is probably not what you want to do here and I would suggest to use vector<int>.

hivert
  • 10,579
  • 3
  • 31
  • 56
  • Why are VLAs allowed to be used within a function in c++14 but not allowed to be passed as a parameter to the function? – b1tchacked Aug 30 '16 at 07:22
1

You can not write such a way the function because arrays even if they have elements of the same type but with different sizes are different types.

You need to write a template function

For example

template <size_t N1, size_t N2>

void mergeSort( int ( &arr1 )[N1], int ( &arr2 )[N2], int ( &result )[N1+N2])
{
    ...
}

Otherwise you need to pass to the function sizes of the arrays. For example

void mergeSort( int arr1[], size_t n1, int arr2[], size_t n2, int result[])
{
    ...
}

In this case it is assumed that the size of array result at least is not less than n1 + n2.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
void mergeSort( int *arr1, int *arr2, int sizeOfArray, int *result[] )
{
...
}
user2076694
  • 806
  • 1
  • 6
  • 10
0

I think the answers above give you what is likely a better solution than what you are doing. But if you absolutely insist on taking arrays (by reference and want to leave the size "unspecified"), do the following:

template <unsigned int SIZE> void mergeSort(int arr1[], int arr2, int (&result)[SIZE]){
}

Then you can pass any size arrays to it and the template argument deduction process will figure out the size at compile time. Please note that this will not work with VLA's if your implementation supports VLA's.