0

How to call an array by reference ?

example : one way how to call an variable by reference can be : void funtion (int&); main(){ int x; funtion (×); return 0; } void funtion(int & x){ } ///////////////////////////////////// if I want to pass an array for value I do it in this way: void arrfuntion(int [], int); int main(){ int n=5, array[n]; arrfuntion(arr,n); return 0; } void arrfuntion(int arr[],int n){ } but my cuestion is : how can I pass an array parameter by reference? whitout using punter by the way.

5th Jun 2018, 2:37 AM
Link Skwor
Link Skwor - avatar
4 Antworten
+ 3
Arrays cannot be passed by value, as an array is identified by the address of the first element, which is passed as the argument. This address cannot be an rvalue.
5th Jun 2018, 2:49 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
When you are passing an int by reference you are doing it to change the value "outside" of the function you are passing it to, correct? Well if you pass an array "by value" you are already doing that. If you change the array inside the function, you are also changing it outside. (This is because an array is just a pointer. A reference is also just a pointer. So from this perspective, they are the same.)
5th Jun 2018, 2:47 AM
Schindlabua
Schindlabua - avatar
+ 1
it is possible to pass arrays by reference. It just requires a different syntax here is an example: https://code.sololearn.com/c4taqIoSHAJ5/#cpp
5th Jun 2018, 4:03 AM
MO ELomari
MO ELomari - avatar
0
really ? oooo wooo I did not realize that, I feel like a dumb ritgh now, millions of thanks for your time and answers.
5th Jun 2018, 2:56 AM
Link Skwor
Link Skwor - avatar