Possible Duplicate:
Pointer vs. Reference
Hi All,
I was trying to explore and I encountered a concern with the reference operator. Consider a normal call by reference swap code as below, which works as desired
#include<iostream>
using namespace std;
void test(int *a,int *b){
int temp;
temp = *a;
*a= *b;
*b= temp;
cout<<"\n Func a="<<*a << " b=" << *b;
}
int main()
{
int a=5,b =3;
cout<<"\n Main a="<<a << " b=" << b;
test(&a,&b);
cout<<"\n Main again a="<<a << " b=" << b;
return 0;
}
On the other hand a code as below also does the same kind of swapping and yield exactly the same result.
#include<iostream>
using namespace std;
void test(int &a,int &b){
int temp;
temp = a;
a= b;
b= temp;
cout<<"\n Func a="<<a << " b=" << b;
}
int main()
{
int a=5,b =3;
cout<<"\n Main a="<<a << " b=" << b;
test(a,b);
cout<<"\n Main again a="<<a << " b=" << b;
return 0;
}
Can some one explain how different is the function call in the second example(first part I am comfortable in which the address is taken as the reference, but what happens in the second case) ?
Within the same line, hope the same happens in an assignment statement as well i.e.
int a=5;
int &b=a;
Thanks in advance.
EDIT:
Thanks for the replies. But my doubt is what exactly happens in the memory
int *pointer=&x
stores the address but what happens when we do
int &point=x.