c++ - Can someone explain me how exactly this code is executed? -
can explain how code executed in here? don't understand how output in second line 8 7 8 , in third line 21 20 21.
#include<iostream> using namespace std; //funtion passed value , reference int fn1(int &a, int b, int *c){ a=b++; b+=7; *c= ++a; return b; } int main(){ int x=4, y=7, z=14; cout<< x<< " " << y<< " "<< z<< endl; // output: 4 7 14 fn1(x,y,&z); cout<< x<< " " << y<< " "<< z<< endl; // output: 8 7 8 (i dont part!!!) x=9, y=12, z=19; y=fn1(x,y,&z); fn1(x,y,&z); cout<< x<< " " << y<< " "<< z<< endl; // output: 21 20 21(i dont part!!!) return 0; }
you creating reference passing b , c's address
a reference x hence change in value of reflected in x since y passed value change in value of b not change y , z address passed change location reflected in z = b++ gets value 7 , b incremented 8(post increment) b+=7 *c = ++a become 8 , assigned address pointed c hence output 8 7 8 x y remain 7 , z c same next 2 calls
Comments
Post a Comment