Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

What's the difference between node, node&, node* and node*&?

OpenStudy (rsmith6559):

node is the actual node node & is a reference to a node node* is a pointer to a node node *& is a reference to a node pointer, a reference to a node declared on the heap

OpenStudy (anonymous):

Can you give me examples on when to use which one?

OpenStudy (queelius):

Pointers: when you need to dynamically allocate memory (do not know how much memory will be needed at compile time). Pointers also have other benefits, e.g., in C/C++ you can perform pointer arithmetic. References: The primary use is to pass objects to functions as a pass-by-reference parameter. Essentially, it's just another name for the object. This has two primary functions: (1) being able to modify the object within the function, (2) being able to avoid memory allocation overhead (as opposed to pass-by-value). A reference to a node pointer: If you pass an object to a function as just a pointer, then you can't change what the original pointer points to -- you can change the value at the memory location that pointer points to, but you can't actually change the value of the pointer (the memory location it is pointing to). However, what if this function is suppose to, say, create a new object and then assign the memory location of this new object to the pointer passed to it? Then, we need to pass the pointer by reference.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!