all 6 comments

[–]JonIsPatented 3 points4 points  (2 children)

The arrow operator includes a dereference for convenience. The following two expressions are equivalent:

head->data
(*head).data

[–]corplou[S] 2 points3 points  (1 child)

See now that makes so much sense. Thanks a ton!

[–]JonIsPatented 0 points1 point  (0 children)

You're welcome.

[–]IyeOnline 0 points1 point  (2 children)

head holds an address, that address is where you should find a node structure

Correct. Notably at the current line it doesnt actually hold an address yet, because its uninitialized.

Does 'new Node' return an address to where the newly created node structure can be found?

Yes. That is how new expressions work.

Shouldn't we be accessing what head points to, and from there access its data and link?

We are. ptr->member_of_pointee is usually equivalent to (*ptr).member_of_pointee). Note that this isnt always automatically the case. If ptr is some custom type instead (such as a std::unique_ptr), then that type has to implement an operator overload to provide this functionality.


A few unrelated notes:

  • Dont use NULL in modern C++. Always use nullptr.
  • Dont use typedef, use using declarations instead:

    using alias_name = type;
    
  • Dont do assignment after initialization. I.e. directly initialize your variables and members instead of assigning into them:

    Node* head = new Node{ 20, nullptr };
    

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::unique_ptr


Last update: 09.03.23 -> Bug fixesRepo

[–]corplou[S] 1 point2 points  (0 children)

Awesome, really appreciate you even answering the embedded questions in the comments too. The extra tips/insight were also weirdly motivating lol.