C ++: difference between ampersands "&" and "asterisk" * in function / method declaration?

Is there any subtle difference between the two:

void a1(float &b) { b=1; }; a1(b); 

and

 void a1(float *b) { (*b)=1; }; a1(&b); 

?

Both of them do the same (or so it seems from main ()), but the first is clearly shorter, however, most of the code that I see uses the second notation. Is there any difference? Maybe in case some object instead of float?

+55
c ++ pointers reference
Feb 27 '09 at 20:49
source share
7 answers

Both do the same, but use references and use pointers.

See my answer here for an exhaustive list of all the differences .

+47
Feb 27 '09 at 20:51
source share

Yes. The designation * states that what goes on the stack is a pointer, that is, the address of something. & says this is a link. The effect is similar but not identical:

Let's take two cases:

  void examP(int* ip); void examR(int& i); int i; 

If I call examP , I write

  examP(&i); 

which takes the address of an element and passes it to the stack. If I call examR ,

  examR(i); 

I do not need it; now the compiler "somehow" passes the link - which practically means that it receives and passes the address i . On the code side, then

  void examP(int* ip){ *ip += 1; } 

I must definitely dereference a pointer. ip += 1 does something completely different.

  void examR(int& i){ i += 1; } 

always updates the value of i .

To think more, read the "call by reference" versus "call by value". The concept & gives a C ++ call by reference.

+21
Feb 27 '09 at 20:59
source share

In the first example with links, you know that b cannot be NULL. With an example pointer, b can be a NULL pointer.

However, note that you can pass a NULL object through the link, but this is inconvenient, and the called procedure may assume that it did so:

 a1(*(float *)NULL); 
+5
Feb 27 '09 at 20:53
source share

In the second example, the caller must prefix the variable name '&' to pass the address of the variable.

This can be an advantage - the caller cannot inadvertently modify a variable, passing it as a reference when they thought they were passing a value.

+3
Feb 27 '09 at 20:53
source share

Besides syntactic sugar, the only real difference is the possibility for a function parameter, which is a pointer to zero. Thus, a version of a pointer can be more expressive if it correctly handles a null register. The zero case can also have some special meaning. The reference version can only work with values ​​of the specified type without zero capability.

+3
Feb 27 '09 at 20:54
source share

Functionally in your example, both versions do the same.

The first has the advantage of being transparent on the call side. Imagine how he will look for the operator:

 cin >> &x; 

And how it looks ugly to call swap

 swap(&a, &b); 

You want to swap a and b. And it looks much better than when you must first accept the address. By the way, bjarne stroustrup writes that the main reason for the links was transparency, which was added on the call side - especially for operators. Also see how it is not obvious whether the next

 &a + 10 

Would add 10 to the content of a by calling its + operator, or add 10 to the temporary pointer to a. Add this to the impossibility so that you cannot overload operators with only built-in operands (for example, a pointer and an integer). Links make this crystal clear.

Pointers are useful if you want your value to be "null":

 a1(0); 

Then, in a1, the method can compare the pointer with 0 and see if the pointer points to any object.

+1
Feb 27 '09 at 20:54
source share

One big difference that is worth noting is what happens outside, you either have:

 a1(something); 

or

 a1(&something); 

I like to pass arguments by reference (always constant one :)) when they do not change in the function / method (and then you can also pass automatically / temporary objects inside) and pass them with a pointer to the notification and notification to the user / reader of the code that calls the method which argument can and possibly is intentionally modified internally.

0
Feb 28 '09 at 15:02
source share



All Articles