- 1

Pass array by reference

How can we pass a static array arr[5]={1,2,3,4,5} as argument to a func by reference?

18th Oct 2021, 2:06 PM
4 Answers
+ 1
Use template: template<size_t len> void f(int (&arr)[len]); Here, len = sizeof(arr) / sizeof(int) i.e. the length of arr.
18th Oct 2021, 2:16 PM
你知道規則,我也是 - avatar
+ 1
Passing array means we are passing the first element address of array which is pass by refrence In the above given example int arr[5]={1,2,3,4,5}; On passing the array in methods we are passing the address of arr[0]
19th Oct 2021, 10:08 AM
sree harsha
sree harsha - avatar
By passing by reference we can avoid null checking...pointers can sometime be null
18th Oct 2021, 6:09 PM
Void BSort(int& a[], int size) { Int temp ; for(int i=0; i<size-1; ++I) { for(int j=I+1; j<size; ++j) { temp=a[I]; a[i]=a[j]; a[j]=temp; } } } int main() { int arr[]={7, 6, 9, 1, 5}; int size = sizeof(arr)/sizeof(a[0]); BSort(&arr[0], size); for(int x : arr) cout<<x<<'\t'; return 0; } This dint work... Pls help in solving pass array by reference... Couldn't find what's wrong with code....
20th Oct 2021, 7:02 AM