+ 4

anyone expalain me how this c++ program works?

PROGRAM: #include <iostream> using namespace std; 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

30th Apr 2017, 4:53 AM
Pravin
Pravin - avatar
4 Antworten
+ 15
Arrays are passed by reference (address), while variables are passed by value to functions. We can see that func1 which receives an array as a parameter is able to change the original value of the array, while func2 does not.
30th Apr 2017, 6:51 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Arrays can actually be modified by a function calls because it's address is passed by reference to the function that calls them while the same cannot be said for variable which is passed as a copy except you explicitly pass it with the reference operator.
30th Apr 2017, 7:59 AM
Aim.
Aim. - avatar
0
Music. Replay
30th Apr 2017, 10:40 AM
claire raimbault
claire raimbault - avatar
0
the array above can be altered by the fun1 but the variable can't
13th May 2017, 8:08 PM
yakubu