Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How does changes in user defined function gets reflected in main function in call by address?

shivajikobardan

Legendary Coder
I've asked my question in the figure below as picture speaks 1000 words.

My question in short is same as title.
g7jEPnOubOg6WKafjV7Xm4o4IlbmMQO-E58ir1tvXAKPaVVCYWtEK-183q_rUki8Goj9ei_LHDe4VFbyAPNv-19WG0QvWWCIiyKqE_mImNe1sLJ3ywI1nnmZpeQd_G95jLMB_8DzGVx2WMpICpEMut8
rUVYsMISB9nOB72GvvXKpzZ1nGnWBqY3HxfYcw_DVeRNB7Yid_HUXmSnGqc9EdvI54SLzlC4WMxu0zmWBN0jzVvvVP2TzTSXbUZLCJrEA4OoHgAGeJ35oAxi-DtO3_2oUUMY2qxMUAwvTPuSdLdvdTE

p_Tg-UIrsLuvfon-Pw9yZRMRVqB4fekKZiZ12TfEZ1ZKzs_QuuNpeONp3rYzL_WFmvJFsmDRvoSPkhrXJo1Q93aXTrgR_3_OI4Mu5flLecFEQ9XrIJ8mJI7Y1Rrw1N2xR9FbnjUg8ILxa_dzM5ibLkQ

zLyr3tIsxBp13-3NbV50gh25N_rn3tUm9hsXjIgrufHstS_gTvl7_MghH7YXfpRhkIRSc0jelRHlTvo6UiqBeVzqtXBXjjvcDt6iVvK61Wnz1DlI0wwPU9sJJGdnalFg6-kAoetvKwyIR29OglRbuqQ

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
 
I've got it.

C++:
int main( void ) {
    int x, y; // two variables, allocated on the stack, say at locations 0x4 and 0x8, values undefined
    change( &x, &y ); // receives 0x4 and 0x8
    return x + y; // accesses memory at 0x4 and 0x8, returning the sum of the values stored there
}

void change( int * a, int * b ) {
    *a = 3; // memory at location 0x4 accessed, value set to 3
    *b = 5; // memory at location 0x8 accessed, value set to 5
}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom