Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function arguments: Pointers vs. References

Posted on 2006-06-23
10
Medium Priority
?
281 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 44

Accepted Solution

by:
zephyr_hex (Megan) earned 200 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 200 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Assisted Solution

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

Assisted Solution

by:Harisha M G
Harisha M G earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

604 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