The prototype of my swap
function is void swap(int *a, int *b)
; why is it invoked when I call swap(a, b)
where a
and b
are integers?
Full program
#include <iostream>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
using namespace std;
int a = 9, b = 8;
cout << "before swap:" << endl;
cout << a << '\t' << b << endl;
swap(a, b);
cout << "after swap:" << endl;
cout << a << '\t' << b << endl;
cin.get();
return 0;
}
Observed behavior
before swap: 9 8 after swap: 8 9
I know it should have been swap(&a,&b)
to call the function swap
. Why does it work nonetheless?