1
#include<iostream>

using namespace std;

void swap(int *x, int *y)
{
    int temp=*x;
    *x=*y;
    *y=temp;
    cout<<*x<<endl<<*y<<endl<<"Bro"<<endl;
}


int main()
{
    int a=10,b=5;
    swap(&a,&b);
    cout<<"HI"<<endl;
    cout<<a<<endl<<b<<endl;
    return 0;
}

and

#include<iostream>

using namespace std;
void swap(int &x, int &y)
{
    int temp=x;
    x=y;
    y=temp;
    cout<<x<<endl<<y<<endl<<"Bro"<<endl;
}


int main()
{
    int a=10,b=5;
    swap(a,b);
    cout<<"HI"<<endl;
    cout<<a<<endl<<b<<endl;
    return 0;
}

Both the codes do give the same output but i don't get the fact that if we are only passing only the variable, how do the addresses get swapped

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 4
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In it they will go into great detail how pointers and references work and how they allow you to affect variables in the call site. – NathanOliver Oct 10 '19 at 20:10
  • Addresses dont get swapped only the values. The main difference is readability here – 463035818_is_not_an_ai Oct 10 '19 at 20:13
  • 1
    When I see `void swap(int *x, int *y)` I think to myself "this routine accepts two parameters, an int pointer or nullptr, and another int pointer or nullptr". But if I pass a nullptr to either parameter, things go all pear-shaped. Whereas `void swap(int &x, int &y)` accepts two mutable references (and as per C++, a reference cannot be a nullptr). – Eljay Oct 10 '19 at 20:14
  • Assigning to a reference has the effect of assigning to the variable it refers to. No addresses are changed in either of your samples. – Peter Oct 10 '19 at 20:15
  • Pointers can point to anywhere, including locations that don't contain variables. References must exist. This is one difference between references and pointers. – Thomas Matthews Oct 10 '19 at 20:15
  • 2
    *"how do the addresses get swapped"* The addresses do not get swapped. The content of the objects at those addresses get swapped. It's not possible to change the address of an object in C++. – François Andrieux Oct 10 '19 at 20:16
  • 2
    Unrelated: There is danger here. In the C++ Standard Library exists a `std::swap` function. It might be brought into your program as a side-effect of including a library header that uses it. The `using namespace std;` will remove the protections of the `std` namespace and put `std::swap` in conflict with your `swap` because `std::swap` can now be referred to as `swap`. – user4581301 Oct 10 '19 at 20:17
  • _"if we are only passing only the variable"_ Neither code block passes only the variable. Both code blocks pass the addresses of the variables to the `swap` function. – JaMiT Oct 10 '19 at 21:08
  • The ease of calling names instead their roll numbers, outcome of both are same. – seccpur Oct 11 '19 at 01:51

1 Answers1

1

Both of the codes do the same thing and both of them use call-by-reference for passing the variables to the swap function. but the methods are different. the first code uses pointers for achieving this goal, while the second code uses references. in the signature of the swap function in the second code you can see the & symbol which means the input arguments must be addresses not just values. by calling the swap function, the addresses of arguments will be passed automatically to the swap function rather than their values. For further reading take a look at the differences between pointers and references.