C pointer pointer, and seg fault

后端 未结 4 1418
不思量自难忘°
不思量自难忘° 2021-01-24 03:47

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

4条回答
  •  梦谈多话
    2021-01-24 04:08

    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.

提交回复
热议问题