Question About Linked Lists Example

This is a discussion on Question About Linked Lists Example within the C Programming forums, part of the General Programming Boards category; Hi, I've been working my way through some examples about how to program C, and I think I'm beginning to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Question About Linked Lists Example

    Hi,

    I've been working my way through some examples about how to program C, and I think I'm beginning to understand the basics of linked lists. In the following tutorial, they use some temporary variables to work their way through a linked list.

    C7

    However, the full code isn't shown, and the link to the example code doesn't work. So I have a question.

    Before this code, they must have declared and allocated memory for "current_node" first, right?

    Code:
    // First deal with the base case, setting the head pointer
    head = (struct list_node *) malloc(sizeof(struct list_node));
    head->node_id = 1;
    
    // Set a temp variable to our current last node
    current_node = head;
    When I made a program based on the example, I did the following first:

    Code:
    struct person *current_node;
    current_node = (struct person *) malloc(sizeof(struct person)); // allocate memory
    Was I right to do this? It seems like I had to. Similarly, at the bottom of the tutorial, they do this:

    Code:
    // Copy the head pointer
    temp = head;
    Again, you would need to do something like this first, right?

    Code:
    struct person *temp;
    temp = (struct person *) malloc(sizeof(struct person)); // allocate memory
    Thanks.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You only do malloc when you want a new node to be added to the list. Here
    Code:
    current_node = head;
    You are only assigning the address where head points to current_node, so no need to make a room for it. It's only pointing to a node for which room has been initially allocated.
    One more thing while doing malloc, no need to cast it, it itself returns void*.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Quote Originally Posted by BEN10 View Post
    You only do malloc when you want a new node to be added to the list. Here
    Code:
    current_node = head;
    You are only assigning the address where head points to current_node, so no need to make a room for it. It's only pointing to a node for which room has been initially allocated.
    One more thing while doing malloc, no need to cast it, it itself returns void*.
    Don't you mean where current_node points to head? Was that a typo, or am I getting mixed up somewhere?

    Also with the malloc thing, I just based my code on the examples. I tried what you said, and it worked. So why do you think they bothered casting it in the examples?

    Thanks for the advice.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bengreenwood View Post
    Don't you mean where current_node points to head? Was that a typo, or am I getting mixed up somewhere?
    Assignments move from right to left, so the value on the right (head) is assigned to the value on the leftg (current_head), so the address where head points is assigned to current_node.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by bengreenwood View Post
    Don't you mean where current_node points to head? Was that a typo, or am I getting mixed up somewhere?

    Also with the malloc thing, I just based my code on the examples. I tried what you said, and it worked. So why do you think they bothered casting it in the examples?

    Thanks for the advice.
    The first part has been mentioned by tabstop. For the malloc thing, I think earlier it would be returning pointer to something else, that's why they typecast it. BTW it's just a wild guess.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bengreenwood View Post
    So why do you think they bothered casting it in the examples?
    Historically, there was no void pointer and the cast was necessary:

    Cprogramming.com FAQ > Casting malloc

    however this is no longer the case.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Thanks for the advice all. Makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding comparing two linked lists
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 05:28 AM
  2. Linked Lists Question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 01:33 AM
  3. Linked Lists Question
    By SlyMaelstrom in forum C++ Programming
    Replies: 12
    Last Post: 11-12-2005, 12:03 PM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 08:15 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

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