Thread: easy question but bugging me on declaring structs

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    easy question but bugging me on declaring structs

    Code:
    // Return the number of nodes in a list (while-loop version)
    int Length(struct node* head) {
    int count = 0;
    struct node* current = head;
    while (current != NULL) {
    count++;
    current = current->next
    }
    return(count);
    }
    why is the struct node* current declared and pass the head pointer? Could we just use the struct node* head to walk through the list?

  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
    Yes you could, in this example.
    But for a more complicated function, it might be useful to always know where head is.
    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
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by danieldcc
    why is the struct node* current declared and pass the head pointer? Could we just use the struct node* head to walk through the list?
    Yes, you could just use “head” to walk the list.

    Perhaps the author didn't like using one name for two semantically distinct entities (even if, technically, they're the same thing). That is, once you do head = head->next it's no longer the head (of the original list) and so is “wrong”.

    Perhaps the author planned on needing the original value after running through the list, although that's not too likely for this function.

    Perhaps the author simply didn't think about the fact that a new variable wasn't needed.

    The end result is, you're right, there's no technical need to do what was done; to know why it was done, you'd have to ask the author.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    Wow, great, I think some of this stuff is finally starting to sink in...thank you.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    you could just declare your struct as a constant in the parameters and then you could just use "current" correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  2. question about declaring function extern
    By movl0x1 in forum C Programming
    Replies: 2
    Last Post: 07-25-2007, 05:16 AM
  3. Difference between typdefing and declaring structs
    By Ganoosh in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2005, 12:11 AM
  4. register structs - problem declaring
    By Kagey in forum C Programming
    Replies: 11
    Last Post: 11-11-2002, 03:58 AM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM