Thread: Trying to print out Linked List but its prints out incorrectly.

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Trying to print out Linked List but its prints out incorrectly.

    The trouble is that my programme is printing out a load of rubbish when I try and print out the Linked List.

    Its printing out a load of characters such as dollar signs and maths symbols. I figure this is because I'm printing out the element out in the wrong format. Can anyone give me a hand figureing out where I am going wrong please?

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct N {
      char      x[1];      
      struct N *next;      
    } List;
    
    List *insertlist(char word[1], List * b)        
    {
      List *c = calloc(1, sizeof(List));
      strcpy( c->x, word ); 
      c->next = b;
      return c;
    }
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
        FILE *fp;
        List *z = NULL;
        int c = 0;
        char x;
        char word[1];
        fp = fopen(argv[1], "r");
        while ( (x = fgetc(fp)) != EOF)
        {
              printf("%c", x);
              if(c==0)
              {
                     if(x == ';')
                     {
                          c = 1;
                          z = insertlist(word, z);
                      }
                      else
                      {
                          z = insertlist(word, z);
                          }
                     }
                     else if(c==1)
                     {
                          if(x == 10)
                          {
                              
                               c = 0;
                               }
                      }
                      
                      
                      }
                      fclose(fp); 
                      
                   while(z->next != NULL) {
                          printf("%c", z);
                          
                          z = z->next;
                          }
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Woah, do
    Code:
    while(z != NULL)
    in that last loop there. Because otherwise if z is null it will try to access z->next and then you will segfault.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Thanks for the reply.

    Tried that but it's still printing out the same rubbish.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    while(z->next != NULL) {
                          printf("%c", z);
    Here z is the pointer to a list node and so can't be printed as a charactor. If you want to print the charactor in the node then use z->X[0]

    Also there's no point in creating arrays with a size of one you might as well just create a single char if thats what you want or if you want a full word in each node then create a char * and assign it's memory dynamically.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    typedef struct N {
      char      x[1];      
      struct N *next;      
    } List;
    
    
    List *insertlist(char word[1], List * b)        
    {
      List *c = calloc(1, sizeof(List));
      strcpy( c->x, word );  
      c->next = b;
      return c;
    }
    You can't strcpy into a location that can only hold a single character - you can but you'd only be able to successfully copy 0 length source strings (NULL terminator only) which would be rather pointless.



    Code:
    List *z = NULL;
    
    ...
    
    printf("%c", z);
    z is a pointer to a List node, IT IS NOT a character. Do you mean instead:
    Code:
    printf("%c",z->x[0]);

    Code:
    while(z->next != NULL)
    {
        ...
    
        z = z->next;
    }
    By reassigning the z pointer, you lose the start of the list and therefore any hopes of freeing the memory you allocated. This is sloppy. You should keep two pointers, one stays pointed to the head of the list, the other is used to walk through the list and help with the printing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Thanks for the replies.

    Ok I did this:

    printf("%c",z->x[0]);

    But now its not printing out anything at all.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char word[1];
    fp = fopen(argv[1], "r");
    while ( (x = fgetc(fp)) != EOF)
    {
        printf("%c", x);
        if(c==0)
        {
            if(x == ';')
            {
                c = 1;
                z = insertlist(word, z);
            }
            else
            {
                z = insertlist(word, z);
            }
        }
        else if(c==1)
        {
            if(x == 10)
            {
                c = 0;
            }
        }
    }
    fclose(fp);
    You never put anything into the word variable. You read from the file into the x variable, but you are attempting to insert word into the list which has nothing (junk) in it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Ah of course, silly error.

    How do I copy the contents of x into the list then?

    If I do this, z = insertlist(x, z);

    It tells me:

    [Warning] passing arg 1 of `insertlist' makes pointer from integer without a cast

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    The problem is that this insert function...


    Code:
    List *insertlist(char word[1], List * b)        
    {
      List *c = calloc(1, sizeof(List));
      strcpy( c->x, word ); 
      c->next = b;
      return c;
    }
    takes char word[1]


    If I change this to word the I get errors with the strcpy bit. How can I sort this out?

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Make your node's x member a single character, not a character array:
    Code:
    typedef struct N {
      char      x;      
      struct N *next;      
    } List;
    2. Make your insert function accept a single character, not a character array:
    Code:
    List *insertlist(char word, List * b)
    3. Remove the strcpy function and use a simple assignment instead:
    Code:
    c->x = word;
    4. The "word" variable in main is superfluous at this point, remove it and simply call the insert function with the variable x instead.
    Code:
    z = insertlist(x, z);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Yep thanks, had all that sorted out before hand.

    Now its still priting out a load of characters.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    ok here is my updated code

    http://pastebin.com/705462

    Here is a picture of the output.
    Last edited by Wiretron; 05-08-2006 at 09:35 AM.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are still doing it

    Code:
    void printlist(List *z) {
        while(z != NULL) {
            printf("%c", z->x );
             z = z->next;
        }
    }
    And you have to work on the logic of reading the file. ( I do not understand what you are trying to do ).
    Kurt
    Last edited by ZuK; 05-08-2006 at 11:20 AM.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    It reads in the file fine because I tested it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM