应用插入函数后,我的链表显示垃圾值

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

我正在尝试创建电话簿,但我的 insert_beg 功能出现问题。该函数的名称本身就很好地解释了它应该做什么。当我使用 create_pd 创建记录时,它起作用了,然后我使用显示功能,然后它显示创建的记录。然后,当我尝试使用 insert_beg 函数并输入我的电话号码和姓名时。当我尝试使用显示功能时,它显示垃圾值。预先感谢我非常感谢任何形式的帮助。

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

在函数 create_pd 中对 scanf 的这些调用是不正确的,并且调用了未定义的行为

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

你必须写

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

写的会更好

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

此外,这些 printf 调用也不正确

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

你必须写

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

您的 printf("\t %s\n", &ptr -> phonenum);scanf("%s", &new_phonedir->phonenum); 调用(电话号码和姓名)有一个额外的 &,这是错误的,因为它获取指向您的指针的地址存储字符数组。这使得 scanf 写入指针值的存储位置,而不是它指向的位置,即指针被损坏,如果输入超过 8 个字节(在 64 位 OS).修复此问题后,当您输入超过 11/15 个字符时,您仍然会 运行 遇到问题,但我想这只是练习代码。您可能还想在 display() 函数中添加一个 while 循环。 :-)

您的第一个问题是一般性问题:编译 C 程序时,您应该始终启用编译器警告。如果你这样做,你的编译器会警告你将错误类型的参数传递给 printfscanf.

在四个地方,您将地址传递给字符串而不是字符串(第一个字符的地址),因此 &ptr->phonenum 应该是 ptr->phonenum

顺便问一下,如果用户输入的 phone 数字超过 10 个字符或名称超过 14 个字符,您认为会发生什么情况?