0

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.

Community
  • 1
  • 1
NirmalGeo
  • 773
  • 4
  • 12
  • 6
    You are asking a very general question, the answer to which would take up a chapter in a book on C++. Read a book. –  May 24 '11 at 08:29
  • Get a good C++ book, they're called "references". They're basically an alias for another variable. – Xeo May 24 '11 at 08:30

3 Answers3

4
  • Both versions perform an identical job and quite probably the compiler will emit identical object code.
  • The version using reference parameters is much easier to read.
  • You can pass a NULL pointer to the version that uses pointers which leads to a memory violation. The same mistake cannot be made with reference parameters.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

& means that you're passing your parameter by reference. The variable you've passed is exactly the same variable you're operating in your function. Actually there is no significant difference between using pointer or reference, because when you passing pointer and then dereference it, you again get exactly the same variable. To sum up: in both cases it's possible to modify passed variable. The opposite, when you pass variables value.

dhblah
  • 9,751
  • 12
  • 56
  • 92
0

In both cases you are passing the variables by reference. In the first function you can conceptually think of the address that is being passed. In the second example though, I conceptually think of the variable itself being passed, but passed by reference instead of by value.

I am not 100% sure, but I suspect on most compilers they would compile to the same object code.

Bingo
  • 3,785
  • 6
  • 23
  • 27