Iterating Linked List

This is a discussion on Iterating Linked List within the C Programming forums, part of the General Programming Boards category; I was wondering why you need to take a copy of the head in an iterating function ? All examples ...

  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
    32,831
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11: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, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21