- 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?
4 odpowiedzi
+ 1
Use template:
template<size_t len>
void f(int (&arr)[len]);
Here, len = sizeof(arr) / sizeof(int) i.e. the length of arr.
+ 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]
0
By passing by reference we can avoid null checking...pointers can sometime be null
0
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....