1

What is the difference in the following code:-

 int a;
 int *p;
 p=&a;
 function(p);

and

 int a;
 function(&a);

I was reading a book where sometimes they have used the first code and sometimes the other. Its not a good book though (by a local author).

Will both codes work the same way or is there any difference?

Also is there any difference in terms of efficiency and does it matter that much?

Thanks

Aditya
  • 91
  • 1
  • 9
  • 1
    There are no references here (unless you have a reference to a pointer), but if you can't add them, use the second. I, at least, when working with that situation, have never had a need to use the first. – chris Jul 17 '12 at 14:21
  • There are no references in your code. In both cases you are passing by pointer. There's no difference for that reason. If you are intending to ask something about references, review your question. – AnT stands with Russia Jul 17 '12 at 14:21
  • Sorry for the mistake, then what is meant by passing by reference? – Aditya Jul 17 '12 at 14:25
  • @Aditya, Great FAQ: http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c – chris Jul 17 '12 at 14:31
  • http://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers here is the when-to-use faq – Sevich Jul 17 '12 at 14:40

4 Answers4

2

For a reference, the object must be already existing in order to reference it. As for a pointer, the object does not need to be already existing when declaring a pointer

Example:

int &i = 10;       //Invalid
const int &i = 10; //Valid

also

you cannot declare an array of references:

int &tab[] = {2,3};  //Invalid
int * tab[] = {2,3}; //Valid

In practice, reference are mostly used as functions parameters and return values;

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
1

As far as I know compiler implements references as pointers. So there should be no difference in performance. But references are more strict and can protect you from making mistakes. For example you can't rebind references or can't perform arithmetic with them

Also some people prefere to pass pointers to the function that modify object. For example

void changeVal(int *p)
{
    *p = 10;
}

They say it's more readable when you see:

   changeVal(&var)

than

changeVal(var);

EDIT

You can think of reference as another name of the object it refers to. So all the changes made to reference are applied to the object. Here is an example:

void foo_copy(int a) //pass by copy
{
    a = 10; //changes copy
}

void foo(int &a) //bass by reference
{ 
     a = 10; //changes passed value
}

void foo(int *a) //pass an adress of a
{
    (*a) = 10; //change a value pointed by a
    a = nullptr; //change a (the pointer). value is not affected
}
Andrew
  • 24,218
  • 13
  • 61
  • 90
  • Yes, the title is misleading. – chris Jul 17 '12 at 14:24
  • If you read the question at face value, it's basically nonsense. I assume @andrew is reading between the lines and this is helpful enough. – tenfour Jul 17 '12 at 14:25
  • @KarolisJuodelė: yeah, i've misunderstood and was mislead by title. But I think I'll leave the answer since it's provide some information the author may interested in – Andrew Jul 17 '12 at 14:27
  • what is meant by passing by reference then? – Aditya Jul 17 '12 at 14:30
0

In above two approaches, Both are using pointers, there is no difference except memory equal to sizeof(int *) will be allocated on stack for "int *p".

In case of call by reference, Refer. It seems you are beginner and learning things, you will come to know more about passing by reference and its use more while using copy constructor in classes.

Community
  • 1
  • 1
jsist
  • 5,223
  • 3
  • 28
  • 43
0

The two code snippets should be equivalent when compiled with a modern compiler. The extra declaration and assignment (p=&a;) is optimised away.

steffen
  • 8,572
  • 11
  • 52
  • 90