Call by address in C/C++, how does it work?

  • Comp Sci
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Work
In summary, the conversation discusses the use of pointers to pass addresses of variables as arguments in a function. The example provided shows how the values at those addresses can be changed by the function, and how they can be accessed in the main function. The use of a debugger can help visualize the actual addresses in memory.
  • #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.
_mImNe1sLJ3ywI1nnmZpeQd_G95jLMB_8DzGVx2WMpICpEMut8.png
JrEA4OoHgAGeJ35oAxi-DtO3_2oUUMY2qxMUAwvTPuSdLdvdTE.png

flLecFEQ9XrIJ8mJI7Y1Rrw1N2xR9FbnjUg8ILxa_dzM5ibLkQ.png

vK61Wnz1DlI0wwPU9sJJGdnalFg6-kAoetvKwyIR29OglRbuqQ.png

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
 
Physics news on Phys.org
  • #2
the common reason i'm reading is "address gets passed so". but I'm failing to realize it mathematically like above.
 
  • #3
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
}
 
  • #4
shivajikobardan said:
I've got it.
Yes, it seems like you do.
shivajikobardan said:
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
}
Your understanding seems fine to me. The only comment I would make is that the compiler would never allocate stack variables at the locations you wrote. The actual locations would be very different. Although it's not crucial to know these addresses, you can see them if you use the debugger.
 

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
864
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
961
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top