Thread: Linked lists, char, char* and printing

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    2

    Exclamation Linked lists, char, char* and printing

    I created a linked list that hold int and char type data. A function adds the data to the list and the other prints it out. When i only print the int type i get no problems but when i try to also print the char type the program crashes.
    So it has to do the way i'm defining the char* in the printing function print_list().
    To be more specific my problem is here in print_list() :

    Code:
    printf("\n [%s] \n", ptr -> name);
    printf("\n [%s] \n", ptr -> lastn);

    So my actual code is (getting 0 errors and 0 warnings but the program crashes):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    // Creating structure for node
    struct test_struct
    {
        int val;         // val is member id number
        char name;
        char lastn;
        int age;
        struct test_struct *next;
    };
    
    
    
    
    // declaring global head and curr pointers
    struct test_struct *head = NULL;
    struct test_struct *curr = NULL;
    
    
    
    
    
    
    // creating a list
    struct test_struct* create_list(int val, char* name, char* lastn, int age)
    {
      printf("\n creating list with head node as [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
    
      struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list
      if(NULL == ptr)
      {
          printf("\n Node creation failed \n");
          return NULL;
      }
    
      ptr->val = val;
      ptr->name = *name;
      ptr->lastn = *lastn;
      ptr->age = age;
      ptr->next = NULL;
    
      head = curr = ptr;
    
      return ptr;
    
    }
    
    
    
    
    
    // add member to list
    struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end)
    {
        if(NULL == head)
        {
            return (create_list(val, name, lastn, age));
        }
    
    
        if(add_to_end)
        {
        printf("\n Adding node to end of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
        }
        else
        {
        printf("\n Adding node to beginning of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
        }
    
    struct test_struct *ptr = malloc(sizeof(struct test_struct));
    
        if(NULL == ptr)
        {
            printf("\n Node creation failed \n");
            return NULL;
        }
    
        ptr->val = val;
        ptr->name = *name;
        ptr->lastn = *lastn;
        ptr->age = age;
        ptr->next = NULL;
    
        if (add_to_end)
        {
            curr-> next = ptr;
            curr = ptr;
        }
        else
        {
            ptr -> next = head;
            head = ptr;
        }
    
        return ptr;
    }
    
    
    
    
    
    
    //printing the list
    void print_list(void)
    {
        struct test_struct *ptr = head;
    
        printf("\n -----Printing list Start----- \n");
    
        while(ptr != NULL)
        {
            printf("\n [%d] \n", ptr -> val);
            printf("\n [%s] \n", ptr -> name);
            printf("\n [%s] \n", ptr -> lastn);
            printf("\n [%d] \n", ptr -> age);
            ptr = ptr->next;
        }
    
        printf("\n -----Printing list end---- \n");
    
        return;
    }
    
    
    
    
    
    // main function
    int main(void)
    {
    
        struct test_struct *ptr = NULL;
    
    
        // for adding member to list
            add_to_list(123, "william", "shakespeare", 30, true);
            add_to_list(124, "william", "gibson", 35, true);
            add_to_list(125, "chuck", "palahniuk", 40, true);
            add_to_list(126, "mario", "puzio", 50, true);
            add_to_list(127, "umberto", "eco", 60, true);
            add_to_list(128, "ezra", "pound", 125, true);
    
            print_list();
    
    
    
    
        return 0;
    }



    Any ideas?

  2. #2
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Members name and lastn of test_struct can only contain a single character. You probably want to make them a little bigger.
    Last edited by Ktulu; 09-24-2015 at 06:51 AM.
    This parameter is reserved

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Mimic03 View Post
    So my actual code is (getting 0 errors and 0 warnings but the program crashes):
    You should increase the warning level of your compiler.
    I get:
    Code:
    $ gcc -Wall -o testii testii.c
    testii.c: In function ‘print_list’:
    testii.c:117:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
             printf("\n [%s] \n", ptr -> name);
             ^
    testii.c:118:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
             printf("\n [%s] \n", ptr -> lastn);
             ^
    testii.c: In function ‘main’:
    testii.c:136:25: warning: unused variable ‘ptr’ [-Wunused-variable]
         struct test_struct *ptr = NULL;
                             ^
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing linked lists
    By ofCircuits in forum C Programming
    Replies: 3
    Last Post: 03-04-2013, 05:08 PM
  2. Fill a linked list char by char
    By w2look in forum C Programming
    Replies: 6
    Last Post: 03-26-2009, 03:07 PM
  3. [Linked Lists] Custom String Class Add Char problems
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2004, 10:15 PM
  4. Printing linked lists
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 03-08-2004, 01:57 AM
  5. linked lists, strcmp(), char array, & a structure
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2003, 06:24 PM

Tags for this Thread