Issues creating and iterating through Linked List?

后端 未结 5 1326
眼角桃花
眼角桃花 2021-01-15 20:10

When I run the program below, it prints \"one: 1\", instead of \"one : 1, two: 2\", as I had expected. Anyone know what\'s going on here? I am trying to create a function th

5条回答
  •  天涯浪人
    2021-01-15 20:43

    To understand the problem in detail, please go through The C Programming Language by Kernighan & Ritchie - Introduction (chapter 1.7/1.8) and Functions and Program Structure (chapter 4).

    Hint: pass-by-value & pass-by-address

    int main() {
        ...
        add(2, "two", &head1);
        ...
    }
    

    OTOH, you should validate the return address of malloc before de-referencing it (in createNewLinkedList).

提交回复
热议问题