Thread: Question regarding linked list

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    22

    Wink Question regarding linked list

    Hello!

    So I'm currently just experimenting with linked lists to get a better understanding of it aswell as on pointers. However, I'm afraid there is something im missing. I'll just post my code and then some comments on what im thinking.
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    
    typedef struct node
    {
        int value;
        struct node *next;
    }node_t;
    
    
    typedef struct list
    {
        node_t *head;
        int size;
    }list_t;
    
    
    void printlist(list)
    {
        node_t *current = list.head;
        while (current != NULL)
        {
            printf("%d", current->value);
            current = current->next;
        }
    }
    
    
    
    
    int main()
    {
        node_t node1;
        node_t node2;    
        
        node1.value = 1;
        node1.next =&node2;
        node2.value = 2;
        node2.next = NULL;
        
        list_t list1;
        list1.head =&node1;
        list1.size= 2;
        
        printlist(list1);
    }
    Okay so what I've done here is that I have a struct for the nodes and the lists. In the main function I have linked them up. Where I'm very wrong is in the printlist function.

    What I want to happen is that the function takes a list as input and then prints each node in the list till it reaches the last node which dosent have a link and therefore the next value is = NULL.

    But this dosen't work in my implementation. I get these errors:
    warning: type of 'list' defaults to 'int' [-Wimplicit-int]
    void printlist(list)

    and:
    request for member 'head' in something not a structure or union
    node_t *current = list.head;

    Okay so what is going on here? Why does my type list default to an int and how is the head not in a structure?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,302
    You forgot about the parameter type, i.e.,
    Code:
    void printlist(list_t list)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    22
    Oh that do make a lot of sense! Thank you for taking the time to answer !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By Annonymous in forum C Programming
    Replies: 2
    Last Post: 10-12-2011, 03:01 PM
  2. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  3. Linked list question.
    By gp364481 in forum C Programming
    Replies: 2
    Last Post: 09-30-2008, 10:46 AM
  4. Linked list question.
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 04-03-2006, 01:49 AM
  5. Linked list question
    By heygirls_uk in forum C Programming
    Replies: 4
    Last Post: 01-12-2004, 06:51 AM

Tags for this Thread