Pass by pointer & Pass by reference [closed]?

Up vote 0 down vote favorite share g+ share fb share tw.

In both cases, I achieved the result. So when is one preferred over the other? What are the reasons we use one over the other?

#include using namespace std; void swap(int* x, int* y) { int z = *x; *x=*y; *y=z; } void swap(int& x, int& y) { int z = x; x=y; y=z; } int main() { int a = 45; int be = 35; cout.

A reference is semantically the following: T& *(T const *) const T& *(T const * const) T&& no C equivalent (C++11) As with other answers, the following from the C++ FAQ is the one-line answer: references when possible, pointers when needed. An advantage over pointers is that you need explicit casting in order to pass NULL. It's still possible, though.

Of the compilers I've tested, none emit a warning for the following: int* p() { return 0; } void x(int& y) { y = 1; } int main() { x(*p); }.

References don't restrict the referring object to referring to something valid. You can return a reference to a local variable, for example. – Seth Carnegie Dec 20 '11 at 5:22 1 It is possible to get a an invalid reference if you dereference a NULL pointer, so you still need to be careful in the calling code.

See stackoverflow.com/questions/57483/… – Mark Ransom Dec 20 '11 at 5:22 It's possible, but unless you're dereferencing unchecked pointers, then it shouldn't happen. And if you don't check pointers before dereferencing them, then I have news for you... – moshbear Dec 20 '11 at 5:24 @moshbear, it was a lesson I had to learn the hard way. And it wasn't even my code!

– Mark Ransom Dec 20 '11 at 5:32 I simplified my answer and gave an example of the infamous null reference. – moshbear Dec 20 '11 at 5:38.

Pass by pointer is the only way you could pass "by reference" in C, so you still see it used quite a bit. The NULL pointer is a handy convention for saying a parameter is unused or not valid, so use a pointer in that case. References can't be updated once they're set, so use a pointer if you ever need to reassign it.

Prefer a reference in every case where there isn't a good reason not to. Make it const if you can.

In fact, the compiler emits the same code for both function and their call. So when it encounters a pass-by-reference function, in the function body when it encounters a parameter, it generates code that silently uses the parameters address and dereferences it. Also when it encounters a call to such a function, instead of passing the parameters' value, it passes in their address.

So basically, the two are not very different. However, if you can use C++ the by reference method is considered more elegant thus that should be preferred. However, in C, there's no passing by reference, so if you want to write C or C-compatible C++ code, you'll have to use the by-pointer notation.

Use references all the time and pointers only when you have to refer to NULL which reference cannot refer. See this FAQ : parashift.com/c++-faq-lite/references.ht....

Int& x = *static_cast(NULL);. That is all. – moshbear Dec 20 '11 at 5:30.

Here is a good article on the matter - "Use references when you can, and pointers when you have to.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions