Home > Back-end >  My linked list is displaying garbage values after my insert function is applied
My linked list is displaying garbage values after my insert function is applied

Time:04-23

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;
}

CodePudding user response:

Your first problem is a general one: When you compile a C program you should always enable compiler warnings. If you do that your compiler will warn you about passing parameters of the wrong type to printf and scanf.

In four places you pass the address to the string instead of the the string (the address to the first character), so &ptr->phonenum should be ptr->phonenum etc.

By the way, what do you think will happen if the user enters a phone number longer than 10 characters or a name longer than 14 characters?

CodePudding user response:

Your printf("\t %s\n", &ptr -> phonenum); and scanf("%s", &new_phonedir->phonenum); calls (for both, phonenum and name) have an additional &, which is wrong, as it gets the address of where the pointer to your char array is stored. This makes the scanf write to where the pointer value is stored, not to where it points to, i.e. the pointer gets corrupted, as well as the data after that if you enter more than 8 bytes (on a 64bit OS). After fixing this you still will run into issues when you enter more than 11/15 chars, but I guess this is just exercise code. You also might want to add a while-loop to your display() function. :-)

CodePudding user response:

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("s", new_phonedir->phonenum);
    printf("Enter name: \n");
    scanf("s", 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);
  • Related