We help IT Professionals succeed at work.

using  pointers vs reference parameters in functions

mitchguy
mitchguy asked
on
I'm a little unclear on what situations make it necessary to use one over the other when passing parameters in functions. I thought that you use reference parameters
when you have a function that needs to return more than one value, but can't you use pointers to accomplish the same task? when do you use one over the other? The only thing i can think of is the extra space required for declaring a pointer which is minimal.
Comment
Watch Question

The the extra memory needs of pointer and refercences is the same (my be you mean call by value ?)
The point is : pointer for c gurus and references for object gurus, becouse you not need to play with the memory to much when use references.
To learn more about this try to use a reference to a pointer (char &*ptr) which is the object oriented style to declare an pointer to pointer :-;
Commented:
When you use refrence you can be sure that your parameter is "real" (points to someting). When using pointer, you can get garbage as well (if it hadn't been initialized).
Note that when using reference you can never get NULL, which is sometimes desired!
I personally always try to use reference, unless I need to get NULL sometimes.

Basically reference is safer than pointer!

Need more?
Use a pointer as the argument type if it makes sense to pass a NULL value. Otherwise, use a reference.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.