0

I have seen some people use the TreeNode* &root while passing as a parameter in function while I use the TreeNode* root.

Is there a difference or is it the same?

  • Does this answer your question? https://stackoverflow.com/questions/823426/passing-references-to-pointers-in-c – Karen Baghdasaryan Jun 24 '22 at 17:13
  • There is in fact an essential difference: `TreeNode*& root` is a reference to a pointer. Changes of `root` will bail out. That's not the case for `TreeNode* root`. The pointer (itself) is passed by value, i.e. changing the pointer has only local effect. Please note, that this is about the pointer itself but not the contents where it points to. – Scheff's Cat Jun 24 '22 at 17:14
  • *I have seen some people use* -- Looks like you're using online coding websites to learn C++. A good C++ book explains what this is -- you won't learn C++ by using sites that parcel out random puzzle questions. – PaulMcKenzie Jun 24 '22 at 17:18
  • 1
    Put another way, parameter `TreeNode* root` is a **copy** of the pointer. `TreeNode* &root` is not a copy. – Drew Dormann Jun 24 '22 at 17:18
  • 1
    The same as the difference between `int` and `int&`. There is nothing special about pointers. – molbdnilo Jun 24 '22 at 17:23

0 Answers0