• 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.
    • 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?


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

My question in short is same as title.



using namespace std;
void change(int*,int*);
int main()
    int x,y;
    cout<<"Enter values  of x and y"<<endl;
    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;
    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;


Enter values of x and y



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.

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
Top Bottom