In the following program, the value of the first element of array x
prints as zero after passing the array as parameter to some function which modifies its parameters. The same does not happen to the int
variable y
, a modification in another function goes unnoticed in the calling function. Thus I was expecting the array to retain its values before the function call, just like it happens with y
. Why is the array changed while the variable is not?
void func1 (int x[]){
x[0]=0;
}
void func2(int y){
y=0;
}
int main(){
int x[]={7}, y=8;
func1(x);
func2(y);
cout << x[0] << y;
return 0;
}
output:
08
expected:
78