explain the code below?

#include<iostream>

using namespace std;

int &bigger(int&,int&);

int main()

{

 int x,y;

 x=1;

 y=2;

 bigger(x,y)=20;

 cout<<x<<endl<<y<<endl;

 return 0;

}

int &bigger(int&a,int&b)

{

 if(a>b)

 return a;

 else

  return b;

}

1 Answer

Relevance
  • EddieJ
    Lv 7
    1 month ago

    This is very unusual code and I've never seen anyone doing anything like this before.

    bigger () returns a pointer to an integer.  Since the arguments to bigger () are passed by reference (because of the & in the parameter list), it returns a pointer to one of them, specifically, the one that points to a larger value.

    In this case, the second argument points to a larger value, so

     bigger(x,y)=20;

    assigns 20 to the second argument, y.

    Offhand, I can't think of a practical reason to do this.

    • Commenter avatarLog in to reply to the answers
Still have questions? Get answers by asking now.