- #1
shivajikobardan
- 674
- 54
- Homework Statement
- How does changes in user defined function gets reflected in main function in call by address?
- Relevant Equations
- none
I've asked my question in the figure below as picture speaks 1000 words.
Code:
Output:
Enter values of x and y
20
30
In change()
Values x=30 y=20
Address x=0x79709ffdbc Address y=0x79709ffdb8
Address x=0x79709ffd90 Address y=0x79709ffd98
In main()
Values x=30 y=20
Address x=0x79709ffdbc Address y=0x79709ffdb8
Code:
Code:
#include<iostream>
using namespace std;
void change(int*,int*);
int main()
{
int x,y;
cout<<"Enter values of x and y"<<endl;
cin>>x>>y;
change(&x,&y);
cout<<"In main()"<<endl;
cout<<"Values x="<<x<<" "<<"y="<<y<<endl;
cout<<"Address x="<<&x<<" "<<"Address y="<<&y<<endl;
return 0;
}
void change(int *a,int *b)
{
int k=*a;
*a=*b;
*b=k;
cout<<"In change()"<<endl;
cout<<"Values x="<<*a<<" "<<"y="<<*b<<endl;
cout<<"Address x="<<a<<" "<<"Address y="<<b<<endl;
cout<<"Address x="<<&a<<" "<<"Address y="<<&b<<endl;
}
Output:
Enter values of x and y
20
30
In change()
Values x=30 y=20
Address x=0x79709ffdbc Address y=0x79709ffdb8
Address x=0x79709ffd90 Address y=0x79709ffd98
In main()
Values x=30 y=20
Address x=0x79709ffdbc Address y=0x79709ffdb8