cstructscanfsingly-linked-listphonebook

My linked list is displaying garbage values after my insert function is applied


I'm trying to create a telephone directory and I'm having issues with my insert_beg function. the function's name itself explains what it is supposed to do pretty much. When I create a record using create_pd it works, and then I use the display function and then it displays the created record. Then when I try to use the insert_beg function and type in my number and name. When I try to use the display function it displays garbage values. Thanks beforehand I really appreciate any sort of help.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct phonedir 
{
    char* name;
    char* phonenum;
    struct phonedir *next;

};


struct phonedir *start = NULL;

void display();
struct phonedir *create_pd(struct phonedir *);
struct phonedir *insert_beg(struct phonedir *);


int main ()
{
    int option;
    while (option != 4)
    {
        printf("\n\n *****MAIN MENU *****");
        printf("\n 1: Create a record");
        printf("\n 2: Display the records");
        printf("\n 3: insert a new record");
        printf("\n 4: EXIT");
        printf("\n\n Enter your option : ");
        scanf("%d", &option);
        switch(option)
        {
            case 1: start = create_pd(start);
            printf("\n PHONE RECORD CREATED");
            break;
            case 2: display(start);
            break;
            case 3: start = insert_beg (start);
            printf("PHONE RECORD ADDED \n");
            break;
        }

    }
    return 0;
}

void display()
{
    struct phonedir *ptr;

    ptr = start;
    if(ptr != NULL)
    {
        printf("\t %s\n", &ptr -> phonenum);
        printf("\t %s", &ptr -> name);
        ptr = ptr -> next;
    }
    else
    {
        printf("Please create an entry\n");
    }
}

struct phonedir *create_pd(struct phonedir *start)
{
    struct phonedir *new_phonedir, *ptr;
    new_phonedir = (struct phonedir *)malloc(sizeof(struct phonedir));

    new_phonedir->phonenum = (char *)malloc(11*sizeof(char));
    new_phonedir->name = (char *)malloc(15*sizeof(char));

    printf("Enter the phone number: \n");
        scanf("%s", &new_phonedir->phonenum);
        printf("Enter name: \n");
        scanf("%s", &new_phonedir->name);

    if (start == NULL)
    {
        new_phonedir->next= NULL;
        start = new_phonedir;

    }
    else
    {
        ptr = start;
        while(ptr->next != NULL)
        {
            ptr = ptr->next;
        }
        ptr->next = new_phonedir;
        new_phonedir->next = NULL;

    }
    return start;
}



struct phonedir *insert_beg(struct phonedir *start)
{
    struct phonedir *new_phonedir;

    new_phonedir = (struct phonedir *)malloc(sizeof(struct phonedir));

    new_phonedir->phonenum = (char *)malloc(11*sizeof(char));
    new_phonedir->name = (char *)malloc(15*sizeof(char));

    printf("Enter phone number: \n");
    scanf("%s", new_phonedir->phonenum);
    printf("Enter name: \n");
    scanf("%s", new_phonedir->name);

    new_phonedir ->next = start;
    start = new_phonedir;
    return start;
}

Solution

  • These calls of scanf within the function create_pd are incorrect and invoke undefined behavior

    printf("Enter the phone number: \n");
        scanf("%s", &new_phonedir->phonenum);
        printf("Enter name: \n");
        scanf("%s", &new_phonedir->name);
    

    you have to write

    printf("Enter the phone number: \n");
        scanf("%s", new_phonedir->phonenum);
        printf("Enter name: \n");
        scanf("%s", new_phonedir->name);
    

    It will be even better to write

    printf("Enter the phone number: \n");
        scanf("%10s", new_phonedir->phonenum);
        printf("Enter name: \n");
        scanf("%14s", new_phonedir->name);
    

    Also these calls of printf are incorrect

        printf("\t %s\n", &ptr -> phonenum);
        printf("\t %s", &ptr -> name);
    

    You have to write

        printf("\t %s\n", ptr -> phonenum);
        printf("\t %s", ptr -> name);