Passing array by reference

Hello, everyone Im writing a simple program where the user will fill an array of integers. The array is then passed to a sort function that I'm writing. Heres the function prototype and function call.

1
2
3
4
5
6
  //prototype:
  void sortArray(int sizeOfArray, int &myArray);
  
  //function call in main()
  sortArray(sizeOfArray, &myArray)


When compiling I'm getting these errors:
1) error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int (*)[(((sizetype)(((ssizetype)sizeOfArray) + -1)) + 1)]'|

2)error: in passing argument 2 of 'void sortArray(int, int&)'|

I've tried to search for a solution but all changes I've made only seemed to create more errors.

Any help would be greatly appreciated, thanks!
In your prototype you declared my array as a reference to single int. But you are trying to pass a pointer to int. You probably want to declare myArray as pointer.
Update: I placed brackets after declaring int &myArray[] in the function protoype and was able to bring it down to only one error.

1
2
3
4
//function prototype 
void sortArray(int sizeOfArray, int &myArray[]);
//function call in main()
sortArray(sizeOfArray, &myArray);


results in the following error:
1)error: declaration of 'myArray' as array of references|



Because you have declared an aray of references and this is illegal. Also you are still passing pointer and not array itself.
Correct way is to do:
1
2
//Note that you have to provide array dimensions here
void sortArray(int sizeOfArray, int (&myArray)[5])
If you use an array reference you need to specify the size, as shown in MiiNiPaa's example. For that reason you can drop the sizeOfArray param.

void sortArrayOf5(int (&myArray)[5]);

If you want your function to handle different size arrays you will need to either use a pointer

void sortArray(int sizeOfArray, int* myArray)

or

void sortArray(int sizeOfArray, int myArray[]) // here [] means a pointer!!

OR make the function a template

1
2
template<int SizeOfArray>
void sortArray(int (&myArray)[SizeOfArray])


If you are using a lot of different sizes of array, the template aproach could lead to code bloat. So could implement the template version using the pointer version.

Andy
Last edited on
Understand. Ill make the corrections suggested. I do have a question though. The size of the array is not known at compile time. I assume that something like void sortArray(int (&myArray)[sizeOfArray]) is illegal. Is there another way of passing the array size to the function? Or should I just declare a constant that holds the array size?
1
2
3
4
//constant declaration
const int MAX_VAL = 100;
//function prototype
void sortArray(int (&myArray)[MAX_VAL])


Then write a check to make sure that the user stays under MAX_VAL?
WHy do you desperately want to pass builtin array by reference? Are there some problems with decay to pointer or something?
Thanks andywestken. Didn't see your post before I wrote mine. That answered a lot of my questions!
Topic archived. No new replies allowed.