21

Can any one tell me the exact diff between call by pointer and call by reference. Actually what is happening on both case?

Eg:

Call By Reference:

void swap(int &x, int &y)
{
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */

   return;
}

swap(a, b);

Call By Pointer:

void swap(int *x, int *y)
{
   int temp;
   temp = *x; /* save the value at address x */
   *x = *y; /* put y into x */
   *y = temp; /* put x into y */

   return;
}

  swap(&a, &b);
8
  • 3
    In java there is no pointers. Commented Jul 2, 2013 at 10:29
  • 2
    Why is it tagged java? Commented Jul 2, 2013 at 10:30
  • 2
    Not a great idea to learn either C++ or Java by comparing them to each other. You'll just keep getting confused by comparing the concepts. Commented Jul 2, 2013 at 10:30
  • 1
    @Baadshah widely off-topic, but java references are more akin to C++ pointers. Commented Jul 2, 2013 at 10:31
  • 1
    "Hello, Java Persons May Know C++ Also.. Tagged to reach some experts" - Well, yeah, and the people on Movies & TV might also know something about C++. Still you wouldn't cross-post this question there, no? Commented Jul 2, 2013 at 10:49

5 Answers 5

22

The both cases do exactly the same.

However, the small difference is, that references are never null (and inside function you are sure, that they are referencing valid variable). On the other hand, pointers may be empty or may point to invalid place in memory, causing an AV.

Sign up to request clarification or add additional context in comments.

3 Comments

What would be the reference of x in the following call: swap(*(int*)0,y)?
@ValeriAtamaniouk that would be undefined behaviour.
@ValeriAtamaniouk What is important, it would be an undefined behavior even before entering the function.
4

Semantically these calls have identical results; under the hood references are implemented with pointers.

The important difference between using references and pointers is with references you have a lot less rope to hang yourself with. References are always pointing to "something", where as pointers can point to anything. For example, it is perfectly possible to do something like this

 void swap(int *x, int *y)
 {
    int temp;
    temp = *x; /* save the value at address x */
    ++x;
    *x = *y; /* put y into x */
    *y = temp; /* put x into y */

    return;
 }

And now this code could do anything, crash, run, have monkeys fly out of your noes, anything.

When in doubt, prefer references. They are a higher, safer level of abstraction on pointers.

Comments

1

In your example, there is no difference.

In C++ reference is implemented using pointers.

Edit: for those who believe reference can't be NULL:

int calc(int &arg)
{
    return arg;
}

int test(int *arg)
{
    return calc(*arg);
}

Guess, what will be the result for test(0)?

9 Comments

... except that the pointers can be NULL and the references cannot...
@trojanfoe Reference can point to NULL. Though it is not a common case.
Please show the code for that.
Dangling references from returning a ref to a local?
Doesn't point to NULL.
|
0

Your code is C++ code. In Java there are no pointers. Both examples do the same thing, functionally there is no difference.

Comments

0

I don't know exactly why this was tagged jave as there are no pointers. However, for the concept you can use both pointers and reference interchangeably if you are working on the value attribute. for example i=4 or *i=4. The advantage of using pointers is that you can manipulate the adress itself. Sometimes you will need to modify the adress (point it to something else ...) this is when pointers are different for reference; pointers allow you to work directly on the adress, reference won't

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.