C pointer pointer, and seg fault

后端 未结 4 1381
不思量自难忘°
不思量自难忘° 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 03:57

    You're setting headRef to hold the address of a variable that lives on the stack; as soon as your Push() function returns, the stack is no longer valid and you can count on it getting overwritten. This is a sure recipe for a segfault.

提交回复
热议问题