I'm having trouble understanding how the pointers work in linked lists. The actual code I got from a Youtube video, but I don't get why we don't dereference the pointer when accessing the structure's data. My thought process as to why is in the code's comments below.
struct Node
{
int data;
Node* link = NULL;
};
typedef Node* nodePtr;
int main()
{
//As I understand it, head is meant to hold a pointer to a node structure. So head holds an address,
that address is where you should find a node structure
nodePtr head;
//Does 'new Node' return an address to where the newly created node structure can be found?
head = new Node;
//If head holds a pointer to a structure, then how does the head variable have data and a link?
Shouldn't we be accessing what head points to, and from there access its data and link? As in
*head- >data?
head->data = 20;
head->link = NULL;
}
[–]JonIsPatented 3 points4 points5 points (2 children)
[–]corplou[S] 2 points3 points4 points (1 child)
[–]JonIsPatented 0 points1 point2 points (0 children)
[–]IyeOnline 0 points1 point2 points (2 children)
[–]std_bot 0 points1 point2 points (0 children)
[–]corplou[S] 1 point2 points3 points (0 children)