Thread: Inserting a name to a linked list

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    4

    Red face Inserting a name to a linked list

    I try to insert a name to a linked list but it doesn't work. Where do i make mistake?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct node{
            
            char *data;
            struct node* next;
    };
    
    
    struct node* start = NULL;
    struct node* temp;
    struct node* iter;
    
    
    
    
    
    
    void addrecord() {
        char *name; 
    printf(" Enter the name: ");
        scanf(" %s",name);
        struct node* newnode= (struct node*)malloc(sizeof(struct node));
        strcpy(newnode->data,name);
    newnode->next = NULL;
    
    if(start == NULL)
    {
    start = newnode;
    }
    else
    {
    
    temp = start;
    while(temp->next != NULL)
    {
    temp = temp->next;
    
    }
    
    temp->next = newnode;
    }
    
    }
    
    int main() {
        
        
        addrecord();
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,624
    You need to malloc space for newnode->data as well, before you copy.
    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
    Apr 2019
    Posts
    4
    Quote Originally Posted by Salem View Post
    You need to malloc space for newnode->data as well, before you copy.
    how?

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Line 25 is a crash. You declared a char* which, initially, points to an arbitrary memory location. You then pass it to scanf which will write to that location. Your compiler should be warning you about this, do not ignore compiler warnings. Similarly, on line 27 you strcpy into an uninitialized pointer in your node structure. Before you tackle data structures, you should go back an review pointers, arrays and memory allocation.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    851
    scanf() expects a pointer to a buffer where the string read from stdin will be copied to. If you do this:
    Code:
    char *name;
    scanf("%s", name);
    scanf will do its work, but you'll get, probably, a "buffer overrun" (no buffer defined by the pointer may poison the stack) or an "segmentation fault".

    Since "name" can have any number of chars, I recommend:
    Code:
    // This will read from stdin allocating the buffer automatically...
    char *name = NULL;
    size_t size = 0;
    
    if ( getline( &name, &size, stdin ) == -1 )
    {
      perror("getline");
      exit(1);
    }
    
    // get rid of the final '\n', if there is one.
    if (size && name[size-1] == '\n') name[size-1]='\0';
    
    // Here name points to a buffer with your string.
    // Need to be free() later.
    getline() is POSIX.1-2008 standard.
    Last edited by flp1969; 04-27-2019 at 02:09 PM.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,905
    You're already using a malloc call for the structure - You are not checking to see if it worked before using it, or freeing it, but it is there

    There is a quick tutorial on linked list here...
    Linked Lists in C - Cprogramming.com

    But if I were you, I'd be reading your course material for malloc - It's apparent that you don't appear to understand it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inserting into a linked list
    By christianB in forum C Programming
    Replies: 3
    Last Post: 06-06-2012, 03:54 PM
  2. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  3. Inserting into linked list
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2010, 07:46 PM
  4. inserting into linked list
    By MiroMage in forum C Programming
    Replies: 4
    Last Post: 09-16-2008, 07:55 PM
  5. linked list inserting
    By comar in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:01 AM

Tags for this Thread