我的链接列表应用了我的插入功能后显示垃圾值

发布于 2025-01-23 12:09:46 字数 2813 浏览 3 评论 0原文

我正在尝试创建一个电话目录,并且我的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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水染的天色ゝ 2025-01-30 12:09:46

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

在四个地方,您将地址传递到字符串而不是字符串(第一个字符的地址),因此&amp; ptr-&gt; phonenum应该为ptr-&gt; phonenum < /code>等。

顺便说一句,如果用户输入超过10个字符或名称长于14个字符的电话号码的电话号码会发生什么?

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?

感情洁癖 2025-01-30 12:09:46

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

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

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);
夏夜暖风 2025-01-30 12:09:46

您的printf(“ \ t%s \ n”,&amp; ptr - &gt; phonenum); and scanf(“%s”,&amp; new_phonedir- phoneenum); 呼叫(既适用于语音和名称)具有额外的&amp;,这是错误的,因为它获取了存储char数组的指针的地址。这使得scanf写入指针值存储的位置,而不是指向的位置,即指针被损坏,以及此后的数据,如果您输入了8个字节以上(在一个64位OS)。修复此操作后,当您输入超过11/15的字符时,您仍然会遇到问题,但我想这只是练习代码。您可能还需要在 -loop中添加 loop到display()函数。 :-)

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. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文