Thread: Iterating Linked List

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    Iterating Linked List

    I was wondering why you need to take a copy of the head in an iterating function ? All examples i have seen do this.

    Won't head be local to this function and the head that was passed will still point to the correct place once it terminates?

    e.g.

    main
    --------

    ret = length(head);

    --------------

    int length(struct tag *head){

    struct tag* current = head;

    ...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The parameter inside the function is already a copy of the head pointer.
    So making another copy (in this case, current) is not necessary, but rather a choice on your part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    Cool, thats what i thought. Its just every example i've looked at recently does this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM