In the below program, I have a function called "change" which take a 2D array as input and return an integer value. I expected that the array is local variable and its value will not be changed in main function, but unexpectedly, it is not the case. Why does this happen?
#include <cstdio>
#include <iostream>
using namespace std;
int change(int arr[2][5]) {
int b;
b = 1;
arr[0][0]=0;
return b;
}
int main(){
int i,j, b;
int arr[2][5] =
{
{1,8,12,20,25},
{5,9,13,24,26}
};
cout << "input array \n";
for (i=0; i<2; i++) {
for (j=0; j<5; j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
b = change(arr);
cout << "final array \n";
for (i=0; i<2; i++) {
for (j=0; j<5; j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
return 0;
}
The output I got is:
input array
1 8 12 20 25
5 9 13 24 26
final array
0 8 12 20 25
5 9 13 24 26