0

I was practising with C++ Pointers and trying different things and came to know about this thing:

The Variables number 1 and number 2 does not has the address they stores integer but the function swap wants two pointers But this works...

But it should only give the answer with the & or pointers. Why This happens and if this is valid then what we call it?

Kindly guide Me!!!!

Complete Code is:

    #include<iostream>
using namespace std;

void swap (int *, int *);


int main()
{
    int number_1 =0;
    int number_2=0;
    
    cout<<"Please Enter The First Number : ";
    cin>>number_1;
    
    cout<<"Please Enter The Second Number : ";
    cin>>number_2;
    
    
    int *ptr_number_1=&number_1;
    int *ptr_number_2=&number_2;
    

    
    cout<<"Before The Function Call Numbers Are : "<<endl;
    cout<<"Number 1 Contains : " << number_1<<endl;
    cout<<"Number 2 Contains : " << number_2<<endl;
    
    cout<<"=================================="<<endl;
    cout<<"Calling the Function with the direct variables : "<<endl;
    cout<<"After The Function Call Numbers Are : "<<endl;
    swap (number_1,number_2);
    cout<<"Number 1 Contains : " << number_1<<endl;
    cout<<"Number 2 Contains : " << number_2<<endl;
    cout<<endl;
    cout<<"=================================="<<endl;
    
    cout<<"=================================="<<endl;
    cout<<"Calling the Function with the Reference Operator : "<<endl;
    cout<<"After The Function Call Numbers Are : "<<endl;
    swap (&number_1,&number_2);
    cout<<"Number 1 Contains : " << number_1<<endl;
    cout<<"Number 2 Contains : " << number_2<<endl;
     cout<<endl;
    cout<<"=================================="<<endl;
    
    cout<<"=================================="<<endl;
    cout<<"Calling the Function with the Pointers  : "<<endl;
    cout<<"After The Function Call Numbers Are : "<<endl;
    swap (ptr_number_1,ptr_number_2);
    cout<<"Number 1 Contains : " << number_1<<endl;
    cout<<"Number 2 Contains : " << number_2<<endl;
     cout<<endl;
    cout<<"=================================="<<endl;
    
    
    

    cout<<endl;
    return 0;
}

void swap (int *number_1, int *number_2){
    
    int temp_number_variable =0;
    temp_number_variable = *number_2;
    *number_2 = *number_1;
    *number_1 = temp_number_variable;
    
}
uh38041
  • 11
  • 4
  • 10
    `swap (number_1,number_2);` works because you're actually calling [`std::swap`](https://en.cppreference.com/w/cpp/algorithm/swap) courtesy of your ill-advised `using namespace std;`, not your `swap` as you may think. Even that won't work on many implementations because `std::swap` mandates including ``, ``, or ``, none of which you've actually included. – WhozCraig Oct 19 '20 at 19:35
  • @WhozCraig Add answer? – Anonymous1847 Oct 19 '20 at 19:44
  • @Anonymous1847 already duped. – WhozCraig Oct 19 '20 at 19:44
  • Since `std::swap` takes its arguments as references and the temporary pointers in `swap (&number_1,&number_2);` wouldn't compile, at least OP:s `swap` is used in at least _one_ place. :-) Edit: It's used in `swap (ptr_number_1,ptr_number_2);` too I see. – Ted Lyngmo Oct 19 '20 at 19:45
  • @ WhozCraig Please Explain a bit i am new to this field. Your explanation will help me a lot. Thanks in advance... – uh38041 Oct 19 '20 at 19:48
  • @uh38041 You'll find the explanation in the link. Just remove `using namespace std;` and start from there to get a quick start. Look at [this](https://godbolt.org/z/fGvvxK) to see where your `swap` is used and where it's not used. – Ted Lyngmo Oct 19 '20 at 19:48

0 Answers0