Solved

Function arguments: Pointers vs. References

Posted on 2006-06-23
10
275 Views
Last Modified: 2010-04-01
In these two function headers:

   void func1( int *pv )
   void func2( int &v )

- What are the major differences?
- What are the key benefits of using one instead of the other?
- Will 'v' in effect be a pointer?

Thanks
0
Comment
Question by:InteractiveMind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 50 total points
ID: 16970305
A pointer is an object containing the address in memory of another object; a
reference is an alias for another object.
A pointer can be null; a reference cannot.
A pointer can be re-seated (i.e., pointed at a different object); a
reference cannot.
A reference must be initialized (i.e., its refer-ee specified) when it is
created; a pointer can be uninitialized.

this may help, too : http://www.cs.nott.ac.uk/~pni/Papers/Tutorials/C++/ref-vs-ptr.html
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 16970616
Internally pointer and reference are implemented by the same way. On Assembly level function has pointer parameter.
However, there is significant difference in C++ compiler level. Reference allows to chain overloaded operators. For example, all overloaded << stream operators return ostream&. This allows to write:

cout << n << m << endl;

cout << n is handled by ostream operator << (int). This operator returms ostream&, which allows to apply the same ostream operator << (int) to the next parameter m etc. Learning overloaded operators, you can see that they return reference to class object, their last line is:
return *this;

This is main advantage of reference. You can see extensive using of references in different C++ libraries, like STL. For simple functions like func1 and func2 the only difference is syntax.
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 50 total points
ID: 16971164
Experts like Herb Sutter, recommend preferring to use reference over pointer, because a reference argument is less ambiguous.

A function that takes a pointer, could be receiving a pointer to a single argument, or an array of objects.
You don't have this abiguity with a reference argument.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 50 total points
ID: 16971437
0
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 50 total points
ID: 16971740
- What are the major differences?
Pointer creates a local variable, that holds the address of the parameter sent.
Reference means an alias

- What are the key benefits of using one instead of the other?
Pointer is compatible with C and many other languages and helpful while writing DLLs.
Also pointer always takes (considering 32 bit machine) 32 bits of memory.
Reference avoids creating another variable, as it is just another name for the original parameter

- Will 'v' in effect be a pointer?
No. v is a reference.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16971949
FYI:
>>Pointer is compatible with C

This is not that important when creating class methods, or pointers to class objects that C can't use anyway.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16972003
Where did I mention classes ?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16972127
>>Where did I mention classes ?

My comment was not meant to contradict your statement, but to give additional information.
Since this is the C++ topic area, I think it's important to point out that compatibility to C should not be a factor if you already have headers that are incompatible to C.

This point may not be obvious to a beginner, or to someone unfamiliar with C.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16972156
Use extern C :)
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 16972506
Thanks very much all; 50 points to all participants.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question