Below is my simple linked list in C. My question is in \"headRef = &newNode;\" which causes segmentation fault. Then I tried instead \"*headRef = newNode;\" which resolves the s
headRef = &newNode
is a local assignment, so the assignment is only valid within the scope of Push
function. If changes to the headRef
should be visible outside the Push
you need to do *headRef = newNode
. Furthermore, these two are not equivalent. headRef = &newNode
assigns the address of a node pointer to a pointer to node pointer while the *headRef = newNode
assigns the address of a node to a pointer to a node using indirection.