0

Why is the code compiling even though I am passing variables to swap function, instead of pointers or addresses?

#include <iostream>
using namespace std;
    
void swap(int *a, int *b)
{
    int *tmp;
    tmp = a;
    a = b;
    b = tmp;
}
    
int main ()
{
    int x = 1; 
    int y = 2; 
    swap (x,y); /*Note: calling swap function using actual variables, not using addresses*/
    cout << " x = " << x << "  y = " << y << endl;
}
9
  • 9
    hint: because of using namespace std. and there's why that's bad Commented Aug 1, 2021 at 20:20
  • 2
    I removed the c tag. Please only tag questions with the language that you're using. Commented Aug 1, 2021 at 20:21
  • 3
    Yet another excellent example to supplement Why is “using namespace std;” considered bad practice?. Commented Aug 1, 2021 at 20:22
  • 1
    @Brian One of the answers to that question mentions this pretty-much exact duplicate. Commented Aug 1, 2021 at 20:26
  • 4
    That is part of it. You may want to consider reading the link that explains why using namespace std is a bad practice. Commented Aug 1, 2021 at 20:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.