C issue - Can't figure how to assign pointer to beginning of list

后端 未结 6 2020
猫巷女王i
猫巷女王i 2021-01-06 03:20

I have a simple assignment that the professor wants us to do. Basically to pull in some numbers from a text file and load into a linked list. I don\'t want to get to much in

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 04:06

    This line:

    lst = lstTemp;  
    

    Only changes the value of lst inside the function. It won't propagate back to the copy of the pointer that the caller has.

    You can either use a pointer-to-a-pointer, or if you can't change the function signature, insert somewhere other than the head of the list.

    Though the typical way of handling this is to not point to the first element in the list - rather, you have some sort of list structure that holds a pointer to the first element, and some other information about the list (say, how many elements it has). Then you pass a pointer to that structure around.

提交回复
热议问题