#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void func(int arr[5], int n1, int n2)
{
cout << "INSIDE func()" << endl;
arr[0]++, arr[3]++;
for(int i = 0; i < 5; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
n1++, n2++;
cout << n1 << ' ' << n2 << endl;
}
int main()
{
int a = 3, b = 4;
int arr[5] = {0,0,0,0,0};
func(arr,a,b);
cout << "INSIDE main()" << endl;
for(int i = 0; i < 5; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
cout << a << ' ' << b << endl;
return(0);
}
So, why does arr[5] changes it's values in func() function, but variables a,b don't. Why does that happen? I know that to a,b change it's values i have to pass the reference of variables a , b to function func().
F.e:
fill(int arr[5], int &n1, int &n2)
{
/* code */
}
But why don't we pass arrays to functions in the same way?
OUTPUT:
INSIDE func()
1 0 0 1 0
4 5
INSIDE main()
1 0 0 1 0
3 4